how to remove spikes from the signal.

21 views (last 30 days)
I have a signal data of ultrasonic sensor. I had to remove the spikes from the plot. how do I do it? I have attached my text file and fig file. I want to remove the spikes from the plot

Accepted Answer

Star Strider
Star Strider on 29 Aug 2017
I am not certain what you want as a result.
This will eliminate ‘spikes’ greater than 10 above than the detrended signal.
D = load('dataee.txt');
I = 1:length(D);
Ddt = detrend(D);
ia = Ddt > 10;
Dnew = D(~ia);
Inew = I(~ia);
figure(1)
plot(Inew, Dnew)
  3 Comments
Star Strider
Star Strider on 30 Aug 2017
My pleasure.
They aren’t straight lines, so you cannot force them to be straight lines. The best you can do is apply a highpass filter to eliminate the low-frequency trends.
Also, you did not supply a time vector or sampling frequency, so I created one. My code should work with your actual time vector without need to change any other parts of my code. (No guarantees.)
I had to completely re-write my code to accommodate your new request. That required both detrending your signal and removing the mean. If the original trend and mean are significant, add them back.
This does what you want:
D = load('dataee.txt');
T = 1:length(D); % Time Vector
Ddt = detrend(D);
Dmean = mean(Ddt);
Ddt = Ddt-Dmean;
ia = Ddt > 10;
Ddt(ia) = Dmean; % Set Spikes = Mean
Dnew = Ddt;
Tnew = T;
figure(1)
plot(Tnew, Dnew) % Time Domain Plot Of Detrended & Despiked Signal
Ts = mean(diff(T)); % Sampling Interval
Fs = 1/Ts; % Sampling Frequency
L = length(Dnew);
FTD = fft(Dnew)/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fs; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
figure(2)
plot(Fv, abs(FTD(Iv))*2)
axis([0 0.1 ylim])
signal = Dnew; % Design & Implement Filter
Ts = mean(diff(T));
Fs = 1/Ts; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
Wp = 0.030/Fn; % Passband Frequency (Normalised)
Ws = 0.032/Fn; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple (dB)
Rs = 50; % Stopband Ripple (dB)
[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs); % Filter Order
[z,p,k] = cheby2(n,Rs,Ws, 'high'); % Filter Design
[sosbp,gbp] = zp2sos(z,p,k); % Convert To Second-Order-Section For Stability
figure(3)
freqz(sosbp, 2^16, Fs) % Filter Bode Plot
filtered_signal = filtfilt(sosbp, gbp, signal); % Filter Signal
figure(4)
plot(Tnew, filtered_signal) % Plot Result
rohith bharadwaj
rohith bharadwaj on 3 Sep 2017
thank you so much I have another question.
https://in.mathworks.com/matlabcentral/answers/355037-i-have-a-signal-with-many-frequencies-how-to-remove-a-particular-frequency-and-reconstruct-the-sign

Sign in to comment.

More Answers (2)

Jan
Jan on 29 Aug 2017
Start with defining mathematically, what a spike is in your case. Does it differ by more than 3 standard deviations from the neighbors? Is it an absolute or relative threshold?
After this is defined, the implementation in Matlab is easy.
  1 Comment
rohith bharadwaj
rohith bharadwaj on 29 Aug 2017
I think I can't use standard deviation.In my signal, if the difference between successive peaks is not zero then it is a spike. for finding peaks I've used findpeaks function.

Sign in to comment.


Saurabh Sinha
Saurabh Sinha on 24 Oct 2018
@Star Strider This is my first time using this forum. I have a similar problem but I have a vertical curve with depth and sample values. Instead of detrending the signal, I want to extract the linear trends in the signal. See the attached image. How do i extract all linear trends from the signal? The trends are of different order i.e. some span over 100 samples, some span over 500 samples and so on.

Tags

Products

Community Treasure Hunt

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

Start Hunting!