Signal Processing Help
3 views (last 30 days)
Show older comments
I am a post-bac research assistant. My background is in physiology. I am new to signal processing. I have been asked to write a script to reduce the noise in a data set and run it on several files.
The process as I understand it... 1. Convert data from time to frequency domain. 2. Filter data with Butterworth filter. 3. Convert data from frequency to time domain.
Example of my code...
N=length(Data);
X = abs(fft(Data,N));
F = [0:N-1];
plot(F,X)
xlabel('Frequency / fs')
ylabel('Amplitude')
[B,A] = butter(2, 0.5);
figure
Xf=filter(B,A,X);
plot(F,Xf,'b-');
title('Filterd Frequency Data')
xlabel('Freq (Hz)')
ylabel('Amplitude')
figure
Nf=length(Xf);
Y = abs(ifft(Xf,Nf));
F = [0:Nf-1];
plot(F,X);
I have tried watching and reading tutorials online but am very lost. Would love help understanding how to use fft and butter. Any tutorials, guides, example code or suggestions would be great! I have the signal processing toolbox. Thank you for your help!
Some questions that come to mind...
- Am I using fft correctly?
- Do I need to use abs or fftshift with fft?
- How do I understand how to set the cutoff frequency?
- How do I determine the order of the Butterworth filter?
- Am I using filter correctly?
- Am I using ifft correctly?
- Do I need to use abs or fftshift with ifft?
0 Comments
Accepted Answer
Wayne King
on 1 Dec 2011
Generally the stricter your filter requirements the higher the order.
The relationship between any linear time invariant operator, like your filter, and the Fourier transform is this one: filtering an input signal with your filter is equivalent to multiplying the Fourier transform of your signal with the Fourier transform of the filter.
Knowing what filter specifications you need is up to you, you have to know what the spectrum of your signal is, what frequency range are you interested in, what frequencies would you like to reject. How much noise is there in the band of frequencies you would like to reject. Those answers are specific to your application.
0 Comments
More Answers (2)
Wayne King
on 29 Nov 2011
Hi, You do not want to use filter() on the frequency domain representation of the data. Use filter() directly on the time domain signal.
output = filter(B,A,data);
It's hard to say whether you have designed your filter correctly because you do not give the relevant details such as the sampling frequency and what frequency you want to filter below (cutoff) frequency.
Your filter:
[B,A] = butter(2, 0.5);
is a very relaxed one, it only attenuates the input by 3 dB at a frequency 1/4 of the sampling rate. Let's assume your data is sampled at 1000 Hz, to see what your filter's magnitude response looks like:
fvtool(B,A,'Fs',1000);
It's basically passing everything from [0,250] Hz unattenuated. What specifications do you want?
Wayne King
on 30 Nov 2011
Hi, you do not need to use ifft(). You do not need to filter the time vector, just the data.
output = filter(B,A,data);
butter() and many other MATLAB filter design functions always use normalized frequency from [0 1] where every frequency in that interval is really 1/10*pi, 1/3*pi, 1/2*pi, 1*pi. In other words whatever number you put in for your cutoff frequency is really that number times pi.
This frequency is in radians/sample. Usually people want to specify their filter design in Hz. You can convert Hz to normalized frequency if you know the sampling frequency.
Wn = (2*f)/Fs;
where Fs is the sampling frequency.
Ex:
Assume the data is sampled at 1 kHz and you want the cutoff frequency at 200 Hz.
Wn = (2*200)/1000;
[B,A] = butter(10,Wn,'low');
fvtool(B,A,'Fs',1000);
Of course you have to determine what order and cutoff frequency make sense for you.
You can use fdesign.lowpass to design your filter directly in Hz.
d = fdesign.lowpass('N,F3db',10,200,1000);
Hd = design(d,'butter');
output = filter(Hd,data);
See Also
Categories
Find more on Get Started with Signal Processing Toolbox 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!