Your goal is unclear to me; here are three different approaches that your question hints at
- filter your raw signal with a band-pass filter as your code attempts to do
- use the
fft
and ifft
functions to filter your raw signal
- calculate the coefficients of a digital filter that would act as a band-pass filter when used with the
filter
function
Approach 1: filtering with bandpass
function
Referring to the bandpass
documentation, you would use your original signal and specify the filter band and sampling frequency as in:
filtered_signal = bandpass(zFiltered,[freq_low freq_high],sample_freq);
plot(zFiltered); hold on;
plot(filtered_signal);
where the inputs are the
zFiltered
: original (unfiltered signal)
freq_low=0.5
: lowest frequency in Hz of the band pass range`
freq_high=3
: highest frequency in Hz of the band pass range
sample_freq=405
: the sampling frequency in Hz
Your approach using digital fast Fourier transform function fft
produces complex results because you are looking at the actual components computed by the transform (which by definition are complex having both real and imaginary parts). You can compute the power of the signal with:
P = abs(res/n).^2;
which gives you the power of the signal for each frequency represented in the Fourier transform (see the fft
function docs here for more information).
Approach 2: Filtering with fft
ifft
functions
Use the Fourier transform and inverse Fourier transform functions to filter the signal. The steps here are to use fft
to get the signal into the frequency domain. Set the elements you want filtered to zero and then apply the inverse Fourier transform (ifft
) to get the filtered signal. Each component represents a given discrete frequencies in the range between 0Hz and F_s/2
that contributes to the signal. Set components of the FFT you want to suppress to zoro and apply ifft
to get back a filter signal. Please see the fft
docs and ifft
docs for information on these functions and some of the intricacies of working with signals in the frequency domain.
Approach 3: Filtering with the coefficeints of the standard difference equation
Computing the coefficients of a digital band pass filter. This approach calculates the coefficients (vectors A
and B
) of the standard difference equation (see filter
docs for details). An example of a function that does this is butter
which gives the coefficients that represent a Butterworth filter of a given frequency. You could also look at the designfilt
function for more options.