Smoothing only a part of a Curve
13 次查看(过去 30 天)
显示 更早的评论
What functions/commands can I use to smooth only a part of my curve? Lets say I have data from x= 0 to x= 500 on the x-axis with corresponding y points. I would like to smooth only data between x= 200 to x= 400, and the rest to be the original data itself. So that sharp edges between x= 0 and x= 200 remains, and then between x= 200 to 400, I get a smooth curve part, and then beyond x= 400 it retains the original shape. There was nothing I could find that would let me smoothen only a part of my curve.
Could someone please help me on this one?
Best regards, Anirudh
0 个评论
回答(1 个)
Image Analyst
2014-2-17
Just smooth everything and replace the smoothed parts, like this code:
fontSize = 25;
% Make sample signal a noisy cosine
x = 1 : 500;
period = 70;
noisySignal = 3*cos(2*pi*x/period) + rand(1, length(x));
subplot(2,2,1);
plot(x, noisySignal, 'b.-');
title('Original Noisy Signal', 'FontSize', fontSize);
grid on;
yl = ylim();
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Apply the Savitzky-Golay filter.
k = 2; % Order of the polynomial
windowSize = 25;
smoothedSignal = sgolayfilt(noisySignal, k, windowSize);
subplot(2, 2, 2);
plot(x, smoothedSignal, 'b.-');
grid on;
title('Savitzky-Golay Filtered Signal', 'FontSize', fontSize);
% Make y limits be the same.
ylim(yl);
% Make a new signal with x=200 to 400 being smooth
newSignal = noisySignal; % Initialize.
newSignal(200:400) = smoothedSignal(200:400);
subplot(2, 2, 3:4);
plot(x, newSignal, 'b.-');
grid on;
title('Composite Signal', 'FontSize', fontSize);
% Make y limits be the same.
ylim(yl);
You might also be interested in this from the Mathworks: http://www.mathworks.com/matlabcentral/answers/103916-how-can-i-smooth-a-signal-while-preserving-peaks-in-matlab-r2013a
3 个评论
Image Analyst
2014-2-20
It's virtually the same code. I don't really care WHERE you get noisy signal - I just made up the cosine curve because I needed some data. Of course you can get it however you need to , such as reading it in from a file, or whatever. After that the code is the same. Basically, filter it all and replace the chunk you want to with the filtered data.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!