How do I filter a signal, using FIR filters.

10 views (last 30 days)
Peter Cristian on 26 Mar 2022
Edited: Scott MacKenzie on 26 Mar 2022
I've got this code, what filter, and how should i use it to only show me in the end the component with the 300Hz frequency? Using a fir filter
```
t = 0:1/8000:1023/8000;
w = 0:8000/1024:4000;
x = 0.5*sin(2*pi*50*t) + 0.5*square(2*pi*130*t) + 0.3*sawtooth(2*pi*250*pi) + 0.2*sin(2*pi*300*t);
h =
y = filter(h,1,x);
figure;
subplot(2,1,1), plot(t,x);
z = abs(fft(x)) / 512;
subplot(2,1,2), plot(w, z(1:513));
figure;
subplot(2,1,1), plot(t,y);
z = abs(fft(y)) / 512;
subplot(2,1,2), plot(w, z(1:513));
```
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Accepted Answer

Scott MacKenzie on 26 Mar 2022
You could perhaps use a butterworth bandpass filter: (Note: Requires Signal Processing Toolbox)
t = 0:1/8000:1023/8000;
w = 0:8000/1024:4000;
x = 0.5*sin(2*pi*50*t) + 0.5*square(2*pi*130*t) + 0.3*sawtooth(2*pi*250*pi) + 0.2*sin(2*pi*300*t);
% h =
% y = filter(h,1,x);
% create bandpass butterworth filter @ 300 Hz
fCutoff1 = 250;
fCutoff2 = 350;
[b, a] = butter(6, [fCutoff1 fCutoff2]/(8000/2), 'bandpass');
% filter the signal
y = filter(b, a, x);
figure;
subplot(2,1,1), plot(t,x);
z = abs(fft(x)) / 512;
subplot(2,1,2), plot(w, z(1:513));
figure;
subplot(2,1,1), plot(t,y);
z = abs(fft(y)) / 512;
subplot(2,1,2), plot(w, z(1:513));
2 CommentsShow NoneHide None
Peter Cristian on 26 Mar 2022
Can i know why, or how did you know the order was 6?
Scott MacKenzie on 26 Mar 2022
Edited: Scott MacKenzie on 26 Mar 2022
I just used order six as it seemed to do a reasonable job in excluding neighboring frequencies. At order 3, for example, you get this:

Sign in to comment.

Categories

Find more on Digital Filter Analysis in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!