I have a signal in Matlab defined by signal
and t
. As it is a noisy signal, I want to delete every component below a certain given frequency "x" Hz with the FFT. How could I do it? I guess the procedure is FFT the signal, delete those components and then apply the IFFT, but I don't know how to implement it. Any help is appreciated!
1 Answer
In fact, deleting a certain frequency range from the FFT equals settings these frequency components to zero. Hence, you end up with the following code:
N = 1000;
signal = randn(N, 1);
N_clear = 490;
% Remove the calls to fftshift, if you want to delete the lower frequency components
S = fftshift(fft(signal));
S_cleared = S;
S_cleared(1:N_clear) = 0;
S_cleared(end-N_clear+2:end) = 0;
S_cleared = fftshift(S_cleared);
signal_cleared = ifft(S_cleared);
subplot(2,2,1);
plot(signal);
title('input signal');
subplot(2,2,2);
plot(abs(S));
title('input spectrum');
subplot(2,2,3);
plot(abs(S_cleared));
title('output spectrum');
subplot(2,2,4);
plot(signal_cleared);
title('output signal');
As you can see, the input signal is very quickly varying. Then, after filtering out almost all higher frequency parts, you end up with a smooth signal.
Note, that normally you use FIR/IIR lowpass/highpass filters for realtime-signal processing, because the FFT is block-based and cannot produce nice transitions between the processed blocks.
-
$\begingroup$ That's perfect, but what do you mean with
N_clear
? If I want to delete frequency components below 1Hz, knowing that f=k*fs/N, with a given sampling frequency, N the length of the signal and k a vector of the length of the signal, I should delete every component k that is under 1Hz from the S vector? Thanks for the answer! @maximilian-matthé $\endgroup$– euskadiCommented Nov 9, 2016 at 15:27 -
$\begingroup$ N_clear is the amount of frequency bins you want to erase. Sure, you can relate this to the actual physical frequencies with the formula you provided. $\endgroup$ Commented Nov 9, 2016 at 17:08
-
$\begingroup$ @MaximilianMatthé, I want to filter out all the frequencies above 80 Hz from my signal. In this case what is N_clear? How should I modify the code to filter out all the higher frequencies? Thank you. $\endgroup$– anepsCommented Sep 27, 2018 at 22:10