smooth vs smoothdata functions

조회 수: 35 (최근 30일)
Christopher Joyce
Christopher Joyce 2021년 3월 30일
댓글: Joshua Jones 2021년 12월 10일
How do these two commands differ?
smooth(y, 'lowess')
smoothdata(y,'lowess')
One of our scripts uses the smooth command, but only some members of our team have the curve fitting toolbox, so I would like to migrate over to the smoothdata command. A quick test of mine shows that the behavior of the two commands is not identical. I'm hoping that some additional options (e.g. a name value pair or window) can yield identical results.
Thank you!
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2021년 3월 31일
hello
FYI
a demo code for various methods of data smoothing that does not require CFT
Fs = 1000;
samples = 1000;
dt = 1/Fs;
t = (0:samples-1)*dt;
y = square(2*pi*3*t) + 0.1*randn(size(t));
% %%%%%%%%%%%%%%%%
figure(1)
N = 10;
ys = slidingavg(y, N);
plot(t,y,t,ys);legend('Raw','Smoothed');
title(['Data samples at Fs = ' num2str(round(Fs)) ' Hz / Smoothed with slidingavg' ]);
% %%%%%%%%%%%%%%%%
figure(2)
N = 10;
ys = medfilt1(y, N,'truncate');
plot(t,y,t,ys);legend('Raw','Smoothed');
title(['Data samples at Fs = ' num2str(round(Fs)) ' Hz / Smoothed with medfilt1' ]);
grid on
%%%%%%%%%%%%%%%%
figure(3)
N = 10;
ys = sgolayfilt(y,3,51);
plot(t,y,t,ys);legend('Raw','Smoothed');
title(['Data samples at Fs = ' num2str(round(Fs)) ' Hz / Smoothed with sgolayfilt' ]);
grid on
%%%%%%%%%%%%%%%%
NN = 4;
Wn = 0.1;
[B,A] = butter(NN,Wn);
figure(4)
ys = filtfilt(B,A,y);
plot(t,y,t,ys);legend('Raw','Smoothed');
title(['Data samples at Fs = ' num2str(round(Fs)) ' Hz / Smoothed with butterworth LP' ]);
grid on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = slidingavg(in, N)
% OUTPUT_ARRAY = SLIDINGAVG(INPUT_ARRAY, N)
%
% The function 'slidingavg' implements a one-dimensional filtering, applying a sliding window to a sequence. Such filtering replaces the center value in
% the window with the average value of all the points within the window. When the sliding window is exceeding the lower or upper boundaries of the input
% vector INPUT_ARRAY, the average is computed among the available points. Indicating with nx the length of the the input sequence, we note that for values
% of N larger or equal to 2*(nx - 1), each value of the output data array are identical and equal to mean(in).
%
% * The input argument INPUT_ARRAY is the numerical data array to be processed.
% * The input argument N is the number of neighboring data points to average over for each point of IN.
%
% * The output argument OUTPUT_ARRAY is the output data array.
%
% © 2002 - Michele Giugliano, PhD and Maura Arsiero
% (Bern, Friday July 5th, 2002 - 21:10)
% (http://www.giugliano.info) (bug-reports to michele@giugliano.info)
%
% Two simple examples with second- and third-order filters are
% slidingavg([4 3 5 2 8 9 1],2)
% ans =
% 3.5000 4.0000 3.3333 5.0000 6.3333 6.0000 5.0000
%
% slidingavg([4 3 5 2 8 9 1],3)
% ans =
% 3.5000 4.0000 3.3333 5.0000 6.3333 6.0000 5.0000
%
if (isempty(in)) | (N<=0) % If the input array is empty or N is non-positive,
disp(sprintf('SlidingAvg: (Error) empty input data or N null.')); % an error is reported to the standard output and the
return; % execution of the routine is stopped.
end % if
if (N==1) % If the number of neighbouring points over which the sliding
out = in; % average will be performed is '1', then no average actually occur and
return; % OUTPUT_ARRAY will be the copy of INPUT_ARRAY and the execution of the routine
end % if % is stopped.
nx = length(in); % The length of the input data structure is acquired to later evaluate the 'mean' over the appropriate boundaries.
if (N>=(2*(nx-1))) % If the number of neighbouring points over which the sliding
out = mean(in)*ones(size(in)); % average will be performed is large enough, then the average actually covers all the points
return; % of INPUT_ARRAY, for each index of OUTPUT_ARRAY and some CPU time can be gained by such an approach.
end % if % The execution of the routine is stopped.
out = zeros(size(in)); % In all the other situations, the initialization of the output data structure is performed.
if rem(N,2)~=1 % When N is even, then we proceed in taking the half of it:
m = N/2; % m = N / 2.
else % Otherwise (N >= 3, N odd), N-1 is even ( N-1 >= 2) and we proceed taking the half of it:
m = (N-1)/2; % m = (N-1) / 2.
end % if
for i=1:nx, % For each element (i-th) contained in the input numerical array, a check must be performed:
if ((i-m) < 1) & ((i+m) <= nx) % If not enough points are available on the left of the i-th element..
out(i) = mean(in(1:i+m)); % then we proceed to evaluate the mean from the first element to the (i + m)-th.
elseif ((i-m) >= 1) & ((i+m) <= nx) % If enough points are available on the left and on the right of the i-th element..
out(i) = mean(in(i-m:i+m)); % then we proceed to evaluate the mean on 2*m elements centered on the i-th position.
elseif ((i-m) >= 1) & ((i+m) > nx) % If not enough points are available on the rigth of the i-th element..
out(i) = mean(in(i-m:nx)); % then we proceed to evaluate the mean from the element (i - m)-th to the last one.
elseif ((i-m) < 1) & ((i+m) > nx) % If not enough points are available on the left and on the rigth of the i-th element..
out(i) = mean(in(1:nx)); % then we proceed to evaluate the mean from the first element to the last.
end % if
end % for i
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Shubham Khatri
Shubham Khatri 2021년 4월 8일
편집: Shubham Khatri 2021년 4월 9일
Hello,
These are very similar codes, doing similar things.Smooth was introduced in 2006, and smoothdata in 2017. Smoothdata is a little more sophisticated, with more options, as you might expect, since it was introduced many years later. It can work on multidimensional arrays. Smooth seems to be for vectors only. The interface is slightly different between them, but not by that much. Both offer a similar set of methods, though smoothdata is a littler broader in choices. They both do essentially the same thing. It would be more important for you to choose intelligently from the various methods offered as options, as that can significantly impact your results. Different methods can be useful for different kinds of noise, different noise structures, so understanding what choices you make is important.
For example, suppose your noise is in the form of rare but very large outliers. In that case, some sort of median smooth makes a lot of sense. Other smoothing operations (moving mean, Savitsky-Golay, etc.) will get draggd around massively by a rare but significant outlier. Again, it is really important to understand the methods in those tools.
For more information about smooth visit the documentation link here
For more information about smoothdata , visit the documentation link here
Hope it helps
  댓글 수: 2
Joshua Jones
Joshua Jones 2021년 12월 7일
I had this same question today while trying to filter my data.
When I use the function 'Smooth' with loess, lowess, rloess, and rlowess I get answers that are somewhat different across the range of my dataset (as I would expect).
However, when I do the same thing for 'SmoothData', I get exactly the same answer with every method. I determined that my answers are the same by subtracting the smoothed data using each method from the smoothed data using the loess results. I get 0 for all points across the range of data for all methods. I am curious if there may be a bug in the code?
I have access to both features and plan to use Smooth for the above reason. I am using the acedemic version R2021a in case this has been addressed in later updates or error reporting.
Joshua Jones
Joshua Jones 2021년 12월 10일
I reached out to technical support and they identified that you have to set the window setting in smoothdata to 5 in order to match the default span setting of smooth.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by