i want to filter a frequeny... ?

1 Ansicht (letzte 30 Tage)
21did21
21did21 am 3 Okt. 2012
Hi all,
I write because I'm trying to do filtering in maltab.
in the code below I have a signal which is composed of two sinus of frequencies 0.3Hz and 2Hz.
I'd like to do is filter the sinus 2Hz the code below does not kidnapped this signal's component ...
you know where it comes from?
clear all;close all;clc;
%%original
t = 0:0.01:10;
y = sin(2*pi*0.3*t)+sin(2*pi*2*t);
%%filter
fs = 1000;
fcoupure = 2*[1 3]/fs;
[b]=fir1(1,fcoupure,'stop');
yfilter=filter(b,1,y);
figure (1);hold on;plot(t,y);plot(t,yfilter,'r');
%%butterworth
fs = 1000;
fcoupure = 2*[1 3]/fs;
[b,a]=butter(1,fcoupure,'stop');
yfilter=filter(b,a,y);
figure (2);hold on;plot(t,y);plot(t,yfilter,'r');

Akzeptierte Antwort

21did21
21did21 am 3 Okt. 2012
Bearbeitet: 21did21 am 3 Okt. 2012
thanks you for your answer !!
but, i am a beginner. can you explain this ?
1°) why we must have "fs = 10" ? because we have a total time of 10 ? (t(end)-t(1) ?
2°) i don't understand this line : "[N,Wn] = buttord(2*[0.5 3.5]/fs,2*[1 3]/fs,0.1,3);"
N ===> is the order of the filter 2*[1 3]/fs ===> is the range of frequencies that i want to cut 0.1 ===> maximal attenuation 3 ===> minimal attenuation
but, what is 2*[0.5 3.5]/fs ??? i don't understand how you choice this ?
why 0.5 and 3.5 ???
=> with you don't choice 0.3 3 for example ?
  2 Kommentare
Walter Roberson
Walter Roberson am 3 Okt. 2012
fs = 10 stands for a sampling frequency of 10 Hz, not for the total time.
Honglei Chen
Honglei Chen am 3 Okt. 2012
You don't have to use fs = 10. It's just for your question, fs = 10 is a good choice. fs = 100 would be a little high and make the filter design difficult
buttord estimates the order of a Butterworth filter. the first argument is passband and the second is stopband. I'm basically choosing the pass band to be between 0 and 0.5 and 3.5 to fs/2 and the stop band to be between 1 and 3 as you did. You could choose 0.3 and 3.2 if you want. This basically impacts the transition band which in turn could affect your filter order.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Honglei Chen
Honglei Chen am 3 Okt. 2012
Bearbeitet: Honglei Chen am 3 Okt. 2012
There are several issues in your code
  1. your sample rate is 100Hz, not 1000Hz as you claimed
  2. you specified a filter order of 1, which may be difficult to achieve what you want to do
  3. your desired frequency is within a few Hz and you are sampling it at 100Hz, you could do what you want with a much lower sample rate, say 10Hz.
Taking Butterworth as example, you should first use buttord to derive the filter order and then pass the order to butter to design the filter
Here is an example using Butterworth with a sample rate of 10Hz
%%original
t = 0:0.1:10;
y = sin(2*pi*0.3*t)+sin(2*pi*2*t);
%%butterworth
fs = 10;
fcoupure = 2*[1 3]/fs;
[N,Wn] = buttord(2*[0.5 3.5]/fs,fcoupure,0.1,3);
[b,a]=butter(N,Wn,'stop');
yfilter=filter(b,a,y);
figure (2);hold on;plot(t,y);plot(t,yfilter,'r');

21did21
21did21 am 4 Okt. 2012
thanks a lot !

Community Treasure Hunt

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

Start Hunting!

Translated by