Codigo Filtros Hann

download Codigo Filtros Hann

of 7

Transcript of Codigo Filtros Hann

  • 7/31/2019 Codigo Filtros Hann

    1/7

    %SimpleLow-PassFilterb=1;a=[1-1];

    %ApplyFilters3_f=filter(b,a,s3);

    %Hanning

    N=20;n=0:N-1;b=0.5*(1-cos(2*pi*n/(N-1)));a=1;

    %ExampleofHowtoUsetheFilterFunction

    %Parameters

    Fs=100;tmax=5;Nsamps=tmax*Fs;

    %CreateInitialSignalst=1/Fs:1/Fs:tmax;

    s1=10*cos(2*pi*t);s2=2*cos(20*pi*t+pi/4);s3=s1+s2;

    %PlotinTimeDomain

    %Originalfigureplot(t,s1)xlabel('Time(s)')ylabel('Amplitude(V)')title('OriginalSignal')ylim([-1515])

    %Original+HighFreqfigureplot(t,s3)xlabel('Time(s)')ylabel('Amplitude(V)')title('OriginalSignalCombinedWithHighFrequencySignal')ylim([-1515])

    %FilterSignals

    %SimpleLow-PassFilterb=1;

    a=[1-1];

    %ApplyFilters3_f=filter(b,a,s3);

    %ScaleOutputs3_f=s3_f/15;

    %PlotFilteredSignalfigure

  • 7/31/2019 Codigo Filtros Hann

    2/7

    plot(t,s3_f)xlabel('Time(s)')ylabel('Amplitude(V)')title('FilteredSignal')ylim([-1515])

    %FrequencyDomain

    f=Fs*(0:Nsamps/2-1)/Nsamps;%Preparefreqdataforplot

    %Original+HighFreqs3_fft=abs(fft(s3));s3_fft=s3_fft(1:Nsamps/2);%DiscardHalfofPoints

    figureplot(f,s3_fft)xlabel('Frequency(Hz)')ylabel('Amplitude')title('FrequencyResponseofCombinedSignalBeforeFiltering')ylim([03000])

    %FilterSignalss3_f_fft=abs(fft(s3_f));s3_f_fft=s3_f_fft(1:Nsamps/2);%DiscardHalfofPoints

    figureplot(f,s3_f_fft)xlabel('Frequency(Hz)')ylabel('Amplitude')title('FrequencyResponseofCombinedSignalAfterFiltering')ylim([03000])

    HANNINGHanningwindow:HANNING(N)returnstheN-pointsymmetricHanningwindowinacolumnvector.Notethatthefirstandlastzero-weightedwindowsamplesarenotincluded.HANNING(N,'symmetric')returnsthesameresultasHANNING(N).HANNING(N,'periodic')returnstheN-pointperiodicHanningwindowandincludesthefirstzero-weightedwindowsample.FIR1FIRfilterdesignusingthewindowmethod:B=FIR1(N,Wn)designsanN'thorderlowpassFIRdigitalfilterandreturnsthefiltercoefficientsinlengthN+1vectorB.Thecut-offfrequencyWnmustbebetween0

  • 7/31/2019 Codigo Filtros Hann

    3/7

  • 7/31/2019 Codigo Filtros Hann

    4/7

    Enterthesamplingfrequency8000

    DesignofFIRfiltersusingwindowsareveryeasyinMATLAB.Followingfunctionsareusedforfindingthepopularwindowcoefficients.

    hann()-forhanningwindowhamming()-forhammingwindowblackman()-forblackmanwindowkaiser()-forkaiserwindow

    HerethedesignofFIRlowpassfilterusinghanningwindowisdemonstrated.Theprogramisgivenbelow

    %ProgramtodesignaFIRfilterusingwindows.

    closeall;clearall;

    fp=input('Enterthepassbandfrequency');fs=input('Enterthestopbandfrequency');rp=input('Enterthepassbandattenuation');

    rs=input('Enterthestopbandattenuation');f=input('Enterthesamplingfrequency');

    %Calculatingfilterorder

    num=-20*log10(sqrt(rp*rs))-13;dem=14.6*(fs-fp)/f;n=ceil(num/dem);n=abs(n);

    %Normalizingthefrequencies

    wp=2*fp/f;

    ws=2*fs/f;wn=(ws+wp)/2;

    %Adjustingthefilterorder.Theorderofwindowmustbeanoddnumber%andtheorderoffiltermustbeonelessthanthatofthewindow

    if(rem(n,2)==0)m=n+1;elsem=n;n=n-1;end

    %Windowsequencecalculation

    w=hann(m);

    %Calculationoffiltercoefficients

    b=fir1(n,wn,'low',w);

    %Plottingthefilterresponse

  • 7/31/2019 Codigo Filtros Hann

    5/7

    freqz(b,1,500,3000);TITLE('MagnitudeandPhaseresponse');

    %output%Enterthepassbandfrequency1000%Enterthestopbandfrequency1200%Enterthepassbandattenuation.2%Enterthestopbandattenuation45%Enterthesamplingfrequency3000

    Youcanchangethislowpassfiltertohighpassfilterbychangingtheoption'low'to'high'inthefir1()function.Theoutputisshownbelow.

    %output%Enterthepassbandfrequency1200%Enterthestopbandfrequency1000

    %Enterthepassbandattenuation.2%Enterthestopbandattenuation45%Enterthesamplingfrequency3000

    w[k+1]=0.5*(1-cos(2*pi*k/(n-1)),k=0,1,..n-1

    ThisDSPMatlabTutorialisaprojectImadeaboutayearagothatisasimpleDSP

    Derivativefilter.ThecoefficientshavebeengeneratedmanuallyandtherestoftheMatlabcodeisplottingtheresultsinthetimedomainaswellasthefrequencydomain.ThefilterisalsowindowedwithaHannwindow...CourtesyofNicefarts:)

    d1=[1/5-1/41/3-1/210-11/2-1/31/4-1/5];d2=[-1/101/9-1/81/7-1/61/5-1/41/3-1/210-11/2-1/31/4-1/51/6-1/71/8-1/91/10];w1=hann(11);w2=hann(21);h1=d1.*w1';h2=d2.*w2';

    %Plot11-tapderivativefunction,windowandwindwedfunction.figure(1)subplot(3,1,1)stem(d1)title('11-tapDerivativefunction.')subplot(3,1,2)stem(w1)title('11-tapHannWindow')subplot(3,1,3)

  • 7/31/2019 Codigo Filtros Hann

    6/7

    stem(h1)title('11-taptransferfunction')

    %PlotFrequencyResponseof11-tapderivative.figure(2)subplot(2,1,1)holdplot((-.5:1/1024:.5-1/1024),fftshift(abs(fft(d1,1024))));plot([-.50.5],[pi0pi],'r');holdofftitle('FrequencyResponseof11-TapDerivativefunction.')subplot(2,1,2)holdplot((-.5:1/1024:.5-1/1024),fftshift(abs(fft(h1,1024))));plot([-.50.5],[pi0pi],'r');holdofftitle('FrequencyResponseof11-TapWindowedDerivativefunction.')holdoff

    %Plot21-tapderivativefunction,windowandwindwedfunction.figure(3)subplot(3,1,1)stem(d2)title('21-tapDerivativefunction.')

    subplot(3,1,2)stem(w2)title('21-tapHannWindow')subplot(3,1,3)stem(h2)title('21-taptransferfunction')

    %PlotFrequencyResponseof21-tapderivative.figure(4)subplot(2,1,1)holdplot((-.5:1/1024:.5-1/1024),fftshift(abs(fft(d2,1024))));plot([-.50.5],[pi0pi],'r');

    holdofftitle('FrequencyResponseof21-TapDerivativefunction.')subplot(2,1,2)holdplot((-.5:1/1024:.5-1/1024),fftshift(abs(fft(h2,1024))));plot([-.50.5],[pi0pi],'r');holdofftitle('FrequencyResponseof21-TapWindowedDerivativefunction.')holdoff

    Fc=0.4;N=100;%FIRfilterorderHf=fdesign.lowpass('N,Fc',N,Fc);

    Hd(1)=design(Hf,'window','window',@hamming);Hd(2)=design(Hf,'window','window',{@chebwin,50});hfvt=fvtool(Hd,'Color','White');legend(hfvt,'Hammingwindowdesign','Dolph-Chebyshevwindowdesign')Hf.FilterOrder=200;Hd(3)=design(Hf,'window','window',{@chebwin,50});set(hfvt,'Filters',[Hd(2)Hd(3)]);legend(hfvt,'Dolph-Chebyshevwindowdesign.Order=100',...'Dolph-Chebyshevwindowdesign.Order=200')Fp=0.38;

  • 7/31/2019 Codigo Filtros Hann

    7/7

    Fst=0.42;%Fc=(Fp+Fst)/2;TransitionWidth=Fst-FpAp=0.06;Ast=60;setspecs(Hf,'Fp,Fst,Ap,Ast',Fp,Fst,Ap,Ast);Hd(4)=design(Hf,'kaiserwin');measure(Hd(4)