此代码应该采用csv数据并获取数据的移动平均值。附件是在java中获得的图的图表。该图与原始图相同。 目标是摆脱压力20以上的所有数据,并使其看起来像方波更平滑。但是在我做了移动平均线之后,它仍然给了我相同的图形。另外,我怎样才能在(0-50)秒之间消除这个尖峰。 移动平均线不起作用。我的移动平均线有问题,但我无法弄清楚。请帮忙。感谢
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
public class MovinAverage {
private static double freqS = 100.000;
private static double sigma = 0.05;
private static ArrayList<Double> noise = null;
static ArrayList<Double> sec = null;
private static double[] Pressure = new double[20481];
private static ArrayList<Double> noisySignal = null;
public static void recordVoice() throws NumberFormatException, IOException{
BufferedReader read = new BufferedReader(new FileReader(new File("C:\\Users\\KwakuK\\Downloads\\smith2.csv")));
String currentLine = new String();
currentLine = read.readLine();
double i = 0;
//make some computation
while((currentLine = read.readLine()) != null)
{
String[] numbers = currentLine.split(","); // split the string into sub strings
if(numbers.length >= 3)
{
System.out.println("currentLine: " + " " + currentLine);
Pressure[(int) i++] = Double.parseDouble(numbers[2]); // when you do the 2, it's the third column which is the pressure
}
}
}
public static void setupFirstPlot() throws FileNotFoundException{
sec = new ArrayList<Double>();
double ws = 1/freqS;
double n = (Pressure.length)*ws;
for(double i = 0.01; i < n; i = i + ws){
sec.add(i);
}
PrintWriter pw = new PrintWriter(new File("plot3.csv"));
for(int i = 0; i < Pressure.length; i++){
pw.write(sec.get(i)+","+Pressure[i]+"\n");
}
pw.close();
}
public static double movingaverage(){
double sum = 0;
for (int i = 0; i < Pressure.length; i++){
sum +=Pressure[i];
}
double average = sum / Pressure.length;
return average;
}
public static void setupNoisySignal(){
noise = new ArrayList<Double>(Pressure.length);
noisySignal = new ArrayList<Double>(Pressure.length);
Random rand = new Random();
for(int i = 0; i < Pressure.length; i++){
double value = rand.nextGaussian()*sigma;
noise.add(value);
noisySignal.add(value + Pressure[i]);
}
}
public static void playSignal(ArrayList<Double> data) throws IOException{
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(byteStream);
for(int i = 0; i < data.size(); i++){
dataStream.writeDouble(data.get(i));
}
dataStream.close();
byteStream.close();
}
public static void main(String[] args) throws NumberFormatException, IOException {
//Populate recording data and plotting data
recordVoice();
setupFirstPlot();
setupNoisySignal();
playSignal(noisySignal);
System.out.println();
System.out.println(movingaverage());
}
}
答案 0 :(得分:0)
在给定窗口上完成移动平均线。因此,您需要将该窗口传递给您的平均方法(并使用它)。所以简而言之,你不会包含所有数据,你会忘记&#34;所有通过该窗口的数据。因此,它是信号中windowLength
最后数据的平均值。
简单移动平均线(SMA)是前n个数据的未加权平均值。
来自https://en.wikipedia.org/wiki/Moving_average
public static double movingaverage(int windowLength) {
double sum = 0;
for (int i = Math.max(0, Pressure.length-windowLength); i < Pressure.length; i++){
sum += Pressure[i];
}
double average = sum / windowLength;
return average;
}