how can i put the type de fir1 window as input in fuction?

2 views (last 30 days)
I have a function which calculates the filter coefficients,return the frequency response .... But i want to see the difference between each type of window. my function is called like this:
[c1 c9 MGhann] =CalculateFilterCoeff(N,D,hann(N),hann(N-1))
and the function is :
%----------------------------------------------------------------------------------------------
%----------------------------------------------------------------------------------------------
function [c1 c9 MargeGain] = CalculateFilterCoeff(Nelec,DinterElec,windtype1,windtype2)
w1= windtype1
w2= windtype2
%%calcul des coefficients des fitres utilisant fir1
c1 =fir1(Nelec-1,2*95*DinterElec,'low',w1);
c9 =fir1(Nelec-2,2*95*DinterElec,'high',w2);
%%Dessin des réponse fréquentielle des filtres appliqués
[h1 w1]=freqz(c1*sqrt(2),1,512);
[h9 w2]=freqz(c9*sqrt(2),1,512);
%%calcul du marge de gain entre Filtre passe-bas et passe-haut
MargeGain = []
y=find(f9>=50, 1, 'first')
MargeGain(1)=20*log10(abs(h9(y)))
end
%------------------------------------------------------------------------------------------------------------------------------------------------------------------------ %------------------------------------------------------------------------------------------------------------------------------------------------------------------------
THE PROBLEM is that i want to prevent to prevent to specify the window length (N) because in my function i want it with different length depending on the value of N. So if N is even: [c1 c9 gain] =CalculateFilterCoeff(N,D,window(N),window(N-1)) else % N is odd [c1 c9 gain] =CalculateFilterCoeff(N,D,window(N),window(N))
I can't but the type of window alone such as [c1 c9 gain] =CalculateFilterCoeff(N,D,hann) and inside the function deside which is the length because it returns that error message: ??? Error using ==> hann at 15 Not enough input arguments.
Thanks for helping to solve this problem
  5 Comments
Maria
Maria on 12 Dec 2013
its in that line where is written :
w1=windtype1(N)
then i use it for the fir1
c1 =fir1(N-1,2*95*D,'low',w1)
Maria
Maria on 12 Dec 2013
Edited: Maria on 12 Dec 2013
lets make it more simple, if i have that function:
function [coef]= CalFilterCoef(N,D,windtype1)
w1=windtype1(N)
c1 =fir1(N-1,2*95*D,'low',w1);
[h1 w1]=freqz(c1*sqrt(2),1,512);
end
when i want to call it by writing in the command window :
CalFilterCoef(20,3e-3,hann)
I have that error ! ??? Error using ==> hann at 15 Not enough input arguments.
I dont want to put the length of window in the function thanks

Sign in to comment.

Accepted Answer

Wayne King
Wayne King on 12 Dec 2013
Edited: Wayne King on 12 Dec 2013
function [c1 c9] =CalculateFilterCoeff(N,D,windtype1)
if (mod(N,2) == 0)
w1= feval(windtype1,N);
w2= feval(windtype1,N-1);
else
w1= feval(windtype1,N);
w2= feval(windtype1,N);
end
c1 =fir1(N-1,2*95*D,'low',w1); % Fcn=Fc*2/Fsech = Fc*2*d
c9 =fir1(N-2,2*95*D,'high',w2);
end
Then call the function like this:
N = 20;
D = 0.003;
[c1 c9 ] = CalculateFilterCoeff(N,D,@hann);
[c1 c9 ] = CalculateFilterCoeff(N,D,@hamming);
Note I removed the MargeGain part because you had some problems there.
  1 Comment
Maria
Maria on 12 Dec 2013
Thank you very much i like this solution ! But i solved it too by declaring the input argument windtype as char ('hann') then i created another m file which uses a switch case of all window type. But urs is easier :) Thank you

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!