How can I smooth this plot?

조회 수: 24 (최근 30일)
Lala0099
Lala0099 2019년 11월 13일
댓글: Star Strider 2019년 11월 15일
So i have a large data set. I just copied here one set, as an example. I tried to use filters to get rid of those small spikes, but i ended up with just a completely straight line.
I split this data in thre sectoins (720 length each) , because I want to kee that stepping characteristic of it.
I am using R2017b edition of matlab, so there are many functions i cannot use to do this.
Could anyone help me with this?

채택된 답변

Star Strider
Star Strider 2019년 11월 13일
Try this:
[D,S] = xlsread('Book1.xlsx');
signal = D(:,1);
L = size(signal,1);
Fs = 1; % Sampling Frequency (Default, Time Vector Or Sampling Frequency Not Provided)
Fn = Fs/2; % Nyquist Frequency
msig = signal - mean(signal);
FTsig = fft(msig)/L; % Subtract Mean (Makes FFT Easier To Interpret)
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FTsig(Iv))*2)
grid
xlim([0 0.05])
% Fs = 2250; % Sampling Frequency
% Fn = Fs/2; % Nyquist Frequency
Wp = 0.019*Fs/Fn; % Stopband Frequency (Normalised)
Ws = 1.2*Wp; % Passband Frequency (Normalised)
Rp = 1; % Passband Ripple
Rs = 60; % Passband Ripple (Attenuation)
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Elliptic Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp,'low'); % Elliptic Filter Design: Zero-Pole-Gain
[sos,g] = zp2sos(z,p,k); % Second-Order Section For Stability
figure
freqz(sos, 2^18, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 Fs/5]) % Optional
set(subplot(2,1,2), 'XLim',[0 Fs/5]) % Optional
signal_filt = filtfilt(sos, g, signal); % Filter Signal
t = linspace(0, 1, L)/Fs; % Time Vector
figure
plot(t, signal, '-b')
hold on
plot(t, signal_filt, '-r', 'LineWidth',1.5)
hold off
grid
xlabel('Frequency')
ylabel('Amplitude')
legend('Original Signal', 'Filtered Signal', 'Location','SW')
It designs a lowpass filter that filters out most of the noise:
How can I smooth this plot - 2019 11 13.png
The stopband ‘Ws’ is set to be a specific multiplier of the passband frequency, so you only need to change ‘Wp’ to get different passband and stopband frequencies.
Experiment to get the result you want.
  댓글 수: 6
Lala0099
Lala0099 2019년 11월 15일
I did more background reading on elliptic filters and now it makes sense! Thank you !
Star Strider
Star Strider 2019년 11월 15일
As always, my pleasure!

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

추가 답변 (1개)

Hank
Hank 2019년 11월 13일
A little more crudely than Strider's answer, since you may not have some of those functions in R2017...
Here is a function that cuts a function up at significant discontinuites and smooths the individual parts. If you don't have the smooth function, replace it with a simple one from the file exchange like https://www.mathworks.com/matlabcentral/fileexchange/19998-fast-smoothing-function
function xs = smoothsmallstuff(x,w)
sig = 6;
x = x(:);
dx = diff(x);
mdx = mean(dx);
sdx = std(dx);
idiscont = [1; find(abs(dx-mdx)/sdx>6); length(x)];
xs = x;
for i = 1:length(idiscont)-1
sect = idiscont(i):idiscont(i+1);
xs(sect) = smooth(x(sect),w);
end
end
  댓글 수: 2
Lala0099
Lala0099 2019년 11월 14일
Thank You for your help!
I tried the smooth function before but it did not work. If i dont have those step like decreases in my output, just a simple signal with spikes, then how could I smooth the signal ?
Hank
Hank 2019년 11월 14일
Did you include the, smoothing width when you used the function?
smooth(x,width) % width = index-width of moving average. Default is 5 points
The default smoothing width is only 5 points. Since your data is really large >1000 points, 5pt smoothing appear effective. Try:
smooth(x,300)
Note this will also smooth out larg discontinuities, hence the function i wrote above.

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

카테고리

Help CenterFile Exchange에서 Smoothing and Denoising에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by