Algorithm for curve smoothing

4 ビュー (過去 30 日間)
Sordin
Sordin 2019 年 5 月 13 日
コメント済み: Sordin 2019 年 5 月 14 日
I have written a simple code that performs a 3-point moving average smoothing algorithm. It is supposed to follow the same algorithm as Matlab's smooth(...) function as described here.
However, the performance of my code is very different from that of Matlab. Matlab's 3-point filter appears to perform a much more aggressive smoothing. Why is that?
Here is my code:
NewSignal = signal;
for i = 2 : length(signal)-1
NewSignal(i,:) = (signal(i,:)+signal(i-1,:)+signal(i+1,:))./3;;
end
And here is how I called Matlab's function:
signal = smooth(time, signal, 3, 'moving');
And here is a comparison of the results:
As one can see, Matlab's function smooths the data a lot further. What is the reason for the discrepancy? How can I modify my code in order for it to perform more like the blue curve?
Any explanation would be greatly appreciated.
I am including the sample data which can be accessed through:
M = csvread('DS0009.csv');
time = M(:,1);
waveform = M(:,2);
  2 件のコメント
gonzalo Mier
gonzalo Mier 2019 年 5 月 13 日
The command that you are using:
waveform = smooth(time,waveform, 5, 'moving');
makes the average of 5 points while your loop use 3 points.
Try with:
waveform = smooth(time,waveform, 3, 'moving');
Sordin
Sordin 2019 年 5 月 14 日
Sorry, that was a typo. I did use '3' and I got the plot shown above.

サインインしてコメントする。

回答 (1 件)

darova
darova 2019 年 5 月 14 日
In this part
NewSignal = signal;
for i = 2 : length(signal)-1
NewSignal(i,:) = (NewSignal(i,:)+NewSignal(i-1,:)+NewSignal(i+1,:))./3;
% NewSignal(i,:) = (signal(i,:)+signal(i-1,:)+signal(i+1,:))./3; % it's different. try it
end
More generic version
NewSignal = signal;
n = 3;
for i = 1 : length(signal)-n+1
s = 0;
for j = 0:n-1
s = s + signal(i+j,:);
end
NewSignal(i,:) = s/n;
end
  1 件のコメント
Sordin
Sordin 2019 年 5 月 14 日
Thanks a lot for attempting to generalize the code. But note that the span is always an odd number. So instead of 1, the start point of the loop has to be 2 for a 3-point filter, or 3 for a 5-point filter, etc.
Also, for some reason your code results in less smoothing than mine. In any case, our code is very different from the result returned by Matlab's smooth(...) function. It is as if it uses a much higher span than 3.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSmoothing and Denoising についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by