How to smooth a curve in Matlab?

2 views (last 30 days)
nancy
nancy on 8 Mar 2017
Answered: Star Strider on 8 Mar 2017
Hi;
I have graphic. But in the graphic, there are a few spikes as it is seen in the attached jpg file. They are showed in black circle. How can I plot the graphic by excluding these spikes? I mean; how can I plot a smooth curve?
I also attach .txt file..
Regards,

Answers (1)

Star Strider
Star Strider on 8 Mar 2017
This will filter out most of the noise. I could not filter out more of it without filtering out parts of your signal.
The Code
d = load('test1.txt');
d1 = d(:,1); % Original Time Vector
d2 = d(:,2); % Original Data Vector
L = length(d1);
tv = linspace(min(d1), max(d1), L); % Time Vector For Interpolation
dv = interp1(d1, d2, tv, 'linear'); % Interpolated Data Vector
Ts = mean(diff(tv)); % Sampling Time Interval
t_stats = [Ts std(tv)];
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
FTD = fft(dv)/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
figure(1)
semilogy(Fv, abs(FTD(Iv))*2) % Plot Fourier Transform
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude')
Wp = 3/Fn; % Low Pass Filter Passband
Ws = 4/Fn; % Low Pass Filter Stopband
Rp=10; % Low Pass Filter Passband Ripple
Rs=30; % Low Pass Filter Stopband Ripple
[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs); % Design Chebyshev Type II Filter
[z,p,k] = cheby2(n,Rs,Ws);
[sos,g] = zp2sos(z,p,k); % Second Order Section For Stability
figure(2)
freqz(sos, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 100]) % ‘Zoom’ X-Axis
set(subplot(2,1,2), 'XLim',[0 100]) % ‘Zoom’ X-Axis
dv_filt = filtfilt(sos, g, dv); % Filter Data
figure(3)
plot(d(:,1), d(:,2))
hold on
plot(tv, dv_filt, 'LineWidth',1)
hold off
grid
xlabel('Time (s)')
ylabel('Amplitude')
legend('Original Signal', 'Filtered Signal')

Community Treasure Hunt

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

Start Hunting!