How can I smooth this plot?

14 views (last 30 days)
Lala0099
Lala0099 on 13 Nov 2019
Commented: Star Strider on 15 Nov 2019
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?

Accepted Answer

Star Strider
Star Strider on 13 Nov 2019
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 Comments
Lala0099
Lala0099 on 15 Nov 2019
I did more background reading on elliptic filters and now it makes sense! Thank you !
Star Strider
Star Strider on 15 Nov 2019
As always, my pleasure!

Sign in to comment.

More Answers (1)

Hank
Hank on 13 Nov 2019
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 Comments
Lala0099
Lala0099 on 14 Nov 2019
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 on 14 Nov 2019
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.

Sign in to comment.

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!