diff options
Diffstat (limited to 'Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok')
9 files changed, 5272 insertions, 0 deletions
diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/10-Design_of_FIR_filters.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/10-Design_of_FIR_filters.ipynb new file mode 100644 index 0000000..a434533 --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/10-Design_of_FIR_filters.ipynb @@ -0,0 +1,664 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 10: Design of FIR filters" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.10: Maximally_Flat_FIR_filter_Desig.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Maximally flat FIR filter design\n", +"Fp=0.2;\n", +"Fs=0.4;\n", +"Fc=0.3;\n", +"Ft=0.2;\n", +"N0=1+0.5/Ft^2;\n", +"N0=ceil(N0);\n", +"alpha=(cos(%pi*Fc))^2;\n", +"k=5;Mmin=14;\n", +"L=Mmin-k;\n", +"N=2*Mmin-1;\n", +"disp(N,'Hence with this length we can get maximally flat FIR filter with no ripples in passband');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.2: Truncation_and_Windowing.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Truncation and Windowing\n", +"//(a)N=9,Barlett Window.\n", +"z=%z;\n", +"Fc=0.25;\n", +"n=-4:4;\n", +"hn=2*Fc*(sinc(0.5*n'*%pi))\n", +"Wn=1-(2*abs(n'))/8//Barlett window\n", +"hwn=hn.*Wn\n", +"Hcz=0;\n", +"for i=1:length(hwn)\n", +" Hcz=Hcz+hwn(i)*(z^((2-i)));\n", +"end\n", +"Hcz//indicates delay of 0.15ms\n", +"//(b)N=6,vonhann Window\n", +"n1=-2.5:2.5;\n", +"hn1=2*Fc*(sinc(0.5*n1'*%pi))\n", +"Wn1=0.5+0.5*(cos(0.4*%pi*n1'))//Vonhann window\n", +"hwn1=hn1.*Wn1\n", +"Hcz1=0;\n", +"for i=1:length(hwn1)\n", +" Hcz1=Hcz1+hwn1(i)*(z^((2-i)));\n", +"end\n", +"Hcz1//1st sample of hwn is 0 hence delay is 1.5ms" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.3ab: FIR_lowpass_Filter_desig.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//FIR filter design using windows\n", +"//(a)Design of FIR filter to meet following specifications\n", +"fp=2;fs=4;Ap=2;As=40;S=20;\n", +"Fp=fp/S;Fs=fs/S;\n", +"Fc=0.15;\n", +"z=%z;\n", +"N1=3.21/(Fs-Fp);\n", +"N1=ceil(N1)\n", +"N2=5.71/(Fs-Fp);\n", +"N2=ceil(N2)\n", +"n1=-16:16;\n", +"n2=-28.5:1:28.5;\n", +"hn1=2*Fc*(sinc(2*Fc*n1'));\n", +"hn2=2*Fc*(sinc(2*Fc*n2'));\n", +"Wn1=0.5+0.5*(cos(2*%pi*n1'/(N1-1)));//Vonhann window\n", +"Wn2=0.42+0.5*(cos(2*%pi*n2'/(N2-1)))+0.08*(cos(4*%pi*n2'/(N2-1)));//Blackman window\n", +"hwn1=abs(hn1.*Wn1);\n", +"hwn2=abs(hn2.*Wn2);\n", +"[hwn1F,fr1]=frmag(hwn1,256);\n", +"[hwn2F,fr2]=frmag(hwn2,256);\n", +"hwn1F1=20*log10(hwn1F);\n", +"hwn2F1=20*log10(hwn2F);\n", +"plot2d(fr1,hwn1F1);\n", +"plot2d(fr2(1:length(fr2)-2),hwn2F1(1:length(fr2)-2));\n", +"xlabel('Digital frequency');\n", +"ylabel('Magnitude [dB]');\n", +"title('Low pass filter using vonhann and Blackmann windows Fc=0.15,vonhann N=33,Blackman N=58');\n", +"//(b)Minimum length design\n", +"Fcv=0.1313;\n", +"Fcb=0.1278;\n", +"Nv=23;Nb=29;\n", +"nv=-11:11;\n", +"nb=-14:14;\n", +"hnv=2*Fcv*(sinc(2*Fcv*nv'));\n", +"hnb=2*Fcb*(sinc(2*Fcb*nb'));\n", +"Wnv=0.5+0.5*(cos(2*%pi*nv'/(Nv-1)));//Vonhann window\n", +"Wnb=0.42+0.5*(cos(2*%pi*nb'/(Nb-1)))+0.08*(cos(4*%pi*nb'/(Nb-1)));//Blackman window\n", +"hwnv=abs(hnv.*Wnv);\n", +"hwnb=abs(hnb.*Wnb);\n", +"[hwnvF,frv]=frmag(hwnv,256);\n", +"[hwnbF,frb]=frmag(hwnb,256);\n", +"hwnvF=20*log10(hwnvF);\n", +"hwnbF=20*log10(hwnbF);\n", +"b=gca();\n", +"xset('window',2);\n", +"plot(frv,hwnvF);\n", +"plot(frb,hwnbF);\n", +"xlabel('Digital frequency');\n", +"ylabel('Magnitude [dB]');\n", +"title('Vonhann Fc=0.1313,Minimum N=23,Blackmann Fc=0.1278,Minimum N=29');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.3cd: FIR_filter_Desig.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Design of high pass FIR filter with specifications\n", +"//fp=4kHZ;fs=2kHZ;Ap=2dB;As=40dB\n", +"fp=2;fs=4;Ap=2;As=40;S=20;\n", +"Fp=fp/S;Fs=fs/S;\n", +"Ft=0.1;\n", +"Fc=0.15\n", +"N1=3.47/(Fs-Fp);//hamming\n", +"N1=int(N1)+1\n", +"N2=5.71/(Fs-Fp);//blackman\n", +"N2=int(N2)+1\n", +"[hn1]=eqfir(N1,[0 0.1;0.2 0.5],[0 1],[1 1]);\n", +"[HF1,fr1]=frmag(hn1,512);\n", +"Hf1=20*log10(HF1);\n", +"[hn2]=eqfir(58,[0 0.1;0.2 0.43],[0 1],[1 1]);\n", +"[HF2,fr2]=frmag(hn2,512);\n", +"Hf2=20*log10(HF2);\n", +"a=gca();\n", +"plot2d(fr1,Hf1,rect=[0 -120 0.5 4]);\n", +"plot2d(fr2(1:length(fr2)-5),Hf2(1:length(fr2)-5),rect=[0 -120 0.5 4]);\n", +"xlabel('Digital Frequency F');\n", +"ylabel('Magnitude [dB]');\n", +"xtitle('High pass filter using Hamming and Blackmann windows LPP Fc=0.35');\n", +"//Minimum Length Design\n", +"[hn3]=eqfir(22,[0 0.1;0.2 0.43],[0 1],[1 1]);\n", +"[HF3,fr3]=frmag(hn3,512);\n", +"Hf3=20*log10(HF3);\n", +"[hn4]=eqfir(29,[0 0.1;0.2 0.5],[0 1],[1 1]);\n", +"[HF4,fr4]=frmag(hn4,512);\n", +"Hf4=20*log10(HF4);\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d(fr3(1:length(fr3)-5),Hf3(1:length(fr3)-5),rect=[0 -120 0.5 4]);\n", +"plot2d(fr4,Hf4,rect=[0 -120 0.5 4]);\n", +"xlabel('Digital Frequency F');\n", +"ylabel('Magnitude [dB]');\n", +"xtitle('Hamming LPP Fc=0.3293 N=22;Blackmann LPP Fc=0.3277 N=29');\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.4a: Half_Band_lowpass_FIR_filter_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Half band FIR Filter Design\n", +"//(a)lowpass Half band Filter\n", +"s=%s;z=%z;\n", +"fp=8;fs=16;Ap=1;As=50;\n", +"S=2*(fs+fp);\n", +"Fp=fp/S;Fs=fs/S;Fc=0.25;\n", +"delp=(10^(Ap/20)-1)/(10^(Ap/20)+1);\n", +"dels=10^(-As/20);\n", +"del=min(delp,dels);\n", +"As0=-20*log10(del)\n", +"N1=(As0-7.95)/(14.36*(Fs-Fp))+1;\n", +"N1=int(N1)+1;\n", +"B=0.0351*(As0-8.7)\n", +"[hn1]=eqfir(19,[0 1/6;1/3 0.5],[1 0],[1 1]);\n", +"[HLPF1,fr1]=frmag(hn1,512);\n", +"HLPf1=20*log10(HLPF1);\n", +"a=gca();\n", +"plot2d(fr1,HLPf1);\n", +"xlabel('Digital Frequency');\n", +"ylabel('Magnitude in dB');\n", +"xtitle('Kaiser half band LPF:B=1.44;Fc=0.25');\n", +"[hn2]=eqfir(21,[0 1/6;1/3 0.5],[1 0],[1 1]);\n", +"[HLPF2,fr2]=frmag(hn2,512);\n", +"HLPf2=20*log10(HLPF2);\n", +"xset('window',1);\n", +"plot2d(fr2,HLPf2);\n", +"xlabel('Digital Frequency');\n", +"ylabel('Magnitude in dB');\n", +"xtitle('Hamming half-band LPF:N=21;Fc=0.25');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.4b: Half_Band_bandstop_FIR_filter_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Half band FIR Filter Design\n", +"//(a)band-stop Half band Filter\n", +"s=%s;z=%z;\n", +"fp1=1;fs1=2;fp2=4;fs2=3;Ap=1;As=50;\n", +"S=2*(fs1+fs2);\n", +"Fp=0.5*(fs2/S-fs1/S);Fs=0.5*(fp2/S-fp1/S);\n", +"Fc=0.5*(Fp+Fs);Fo=0.25;\n", +"delp=(10^(Ap/20)-1)/(10^(Ap/20)+1);\n", +"dels=10^(-As/20);\n", +"del=min(delp,dels);\n", +"As0=-20*log10(del)\n", +"N1=(As0-7.95)/(14.36*(Fs-Fp))+1;\n", +"N1=ceil(N1);\n", +"B=0.0351*(As0-8.7)\n", +"[hn1]=eqfir(31,[0 0.1;0.2 0.3;0.4 0.5],[1 0 1],[1 1 1]);\n", +"[HBSF1,fr1]=frmag(hn1,400);\n", +"HBSf1=20*log10(HBSF1);\n", +"a=gca();\n", +"plot2d(fr1,HBSf1);\n", +"xlabel('Digital Frequency');\n", +"ylabel('Magnitude in dB');\n", +"xtitle('Kaiser half band LPF:B=1.44;Fc=0.25');\n", +"[hn2]=eqfir(35,[0 0.1;0.2 0.3;0.4 0.5],[1 0 1],[1 1 1]);\n", +"[HF2,fr2]=frmag(hn2,200);\n", +"HBSf2=20*log10(HF2);\n", +"xset('window',1);\n", +"plot2d(fr2,HBSf2);\n", +"xlabel('Digital Frequency');\n", +"ylabel('Magnitude in dB');\n", +"xtitle('Hamming half-band LPF:N=21;Fc=0.25');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.5a: Design_by_Frequency_Sampling.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Fir low pass filter design by frequency sampling\n", +"z=%z;\n", +"N=10;\n", +"magHk=[1 1 1 0 0 0 0 0 1 1];\n", +"k=[0:7 -1 -2];\n", +"fik=-%pi*k'*(N-1)/N;\n", +"for i=1:length(fik)\n", +" H1k(i)=magHk(i)*exp(%i*fik(i));\n", +"end\n", +"H1n=(dft(H1k,1));\n", +"H2k=H1k;\n", +"H2k(3)=0.5*%e^(-%i*1.8*%pi);\n", +"H2k(9)=0.5*%e^(%i*1.8*%pi);\n", +"H2n=(dft(H2k,1));\n", +"H1Z=0;H2Z=0;\n", +"for i=1:length(H1n)\n", +" H1Z=H1Z+H1n(i)*z^(-i); \n", +"end\n", +"for i=1:length(H2n)\n", +" H2Z=H2Z+H2n(i)*z^(-i); \n", +"end\n", +"F=0:0.01:1;\n", +"F1=0:0.1:0.9;\n", +"H1F=abs(horner(H1Z,exp(%i*2*%pi*F')));\n", +"H2F=abs(horner(H2Z,exp(%i*2*%pi*F')));\n", +"a=gca();\n", +"plot2d(F1,magHk);\n", +"plot2d(F,H2F);\n", +"plot2d(F,H1F);\n", +"xlabel('Digital Frequency F');\n", +"ylabel('magnitude');\n", +"xtitle('Low pass filter using frequency sampling');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.5b: Design_by_Frequency_Sampling.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Fir high pass filter design by frequency sampling\n", +"z=%z;\n", +"N=10;\n", +"magHk=[0 0 0 1 1 1 1 1 0 0];\n", +"k=[0:5 -4:-1:-1];\n", +"fik=(-%pi*k'*(N-1)/N)+(0.5*%pi);\n", +"for i=1:length(fik)\n", +" H1k(i)=magHk(i)*exp(%i*fik(i));\n", +"end\n", +"H1n=(dft(H1k,1));\n", +"H2k=H1k;\n", +"H2k(3)=0.5*%e^(-%i*1.3*%pi);\n", +"H2k(9)=0.5*%e^(%i*1.3*%pi);\n", +"H2n=(dft(H2k,1));\n", +"H1Z=0;H2Z=0;\n", +"for i=1:length(H1n)\n", +" H1Z=H1Z+H1n(i)*z^(-i); \n", +"end\n", +"for i=1:length(H2n)\n", +" H2Z=H2Z+H2n(i)*z^(-i); \n", +"end\n", +"F=0:0.01:1;\n", +"F1=0:0.1:0.9;\n", +"H1F=abs(horner(H1Z,exp(%i*2*%pi*F')));\n", +"H2F=abs(horner(H2Z,exp(%i*2*%pi*F')));\n", +"a=gca();\n", +"plot2d(F1,magHk);\n", +"plot2d(F,H2F);\n", +"plot2d(F,H1F);\n", +"xlabel('Digital Frequency F');\n", +"ylabel('magnitude');\n", +"xtitle('Low pass filter using frequency sampling');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.6a: Optimal_FIR_Bandstop_Filter_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//optimal Fir band stop filter design\n", +"fp1=1;fp2=4;fs1=2;fs2=3;\n", +"Ap=1;As=50;S=10;\n", +"Fp1=fp1/S;Fp2=fp2/S;Fs1=fs1/S;Fs2=fs2/S;\n", +"FT=0.1;FC=0.25\n", +"//calculation of filter length\n", +"delp=(10^(Ap/20)-1)/(10^(Ap/20)+1);\n", +"dels=10^(-As/20);\n", +"N=1+((-10*log10(delp*dels)-13)/(14.6*FT))\n", +"N1=21;\n", +"[hn]=eqfir(N1,[0 0.1;0.2 0.3;0.4 0.5],[1 0 1],[1 1 1]);\n", +"[HF,fr]=frmag(hn,512);\n", +"Hf=20*log10(HF);\n", +"a=gca();\n", +"plot(fr,Hf);\n", +"xlabel('Digital frequency F');\n", +"ylabel('Magnitude in dB');\n", +"xtitle('optimal BSF:N=21;Ap=0.2225;As=56.79dB');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.6b: Optimal_Half_Band_Filter_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//optimal Fir band pass filter design\n", +"fp=8;fs=16;\n", +"Ap=1;As=50;S=48;\n", +"Fp=fp/S;Fs=fs/S;\n", +"FT=0.1;FC=0.25\n", +"//calculation of filter length\n", +"delp=(10^(Ap/20)-1)/(10^(Ap/20)+1);\n", +"dels=10^(-As/20);\n", +"del=min(delp,dels);\n", +"N=1+((-10*log10(del*del)-13)/(14.6*FT));\n", +"N1=19;\n", +"[hn]=eqfir(N1,[0 1/6;1/3 0.5],[1 0],[1 1]);\n", +"[HF,fr]=frmag(hn,200);\n", +"Hf=20*log10(HF);\n", +"a=gca();\n", +"plot(fr,Hf);\n", +"xlabel('Digital frequency F');\n", +"ylabel('Magnitude in dB');\n", +"xtitle('optimal Half band LPF N=17');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.7: Multistage_Interpolation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//The concept of multistage Interpolation\n", +"//(a)Single stage interpolator\n", +"Sin=4;Sout=48;\n", +"fp=1.8;\n", +"fs=Sin-fp;\n", +"FT=(fs-fp)/Sout;\n", +"disp('By using single stage the total filter length is:') \n", +"L=4/FT\n", +"//(b)Two-stage interpolator\n", +"Sin=[4 12];\n", +"I=[3 4];//interpolating factors\n", +"Sout=[12 48];\n", +"fp=[1.8 1.8];\n", +"fs=Sin-fp;\n", +"L1=4*Sout./(fs-fp);\n", +"L=0;\n", +"for i=1:length(L1)\n", +" L=L+L1(i);\n", +"end\n", +"disp('By using 2 stage interpolator filter length is:')\n", +"ceil(L)\n", +"//(c)3 stage interpolator with I1=2;I2=3;I3=2\n", +"Sin=[4 8 24];\n", +"I=[2 3 2];\n", +"Sout=[8 24 48];\n", +"fp=[1.8 1.8 1.8];\n", +"fs=Sin-fp;\n", +"L2=4*Sout./(fs-fp);L=0;\n", +"for i=1:length(L2)\n", +" L=L+L2(i);\n", +"end\n", +"disp('By using 3 stage interpolator filter length is:')\n", +"ceil(L)\n", +"//(d)3 stage interpolator with I1=2;I2=3;I3=2\n", +"Sin=[4 12 24];\n", +"I=[3 2 2];\n", +"Sout=[12 24 48];\n", +"fp=[1.8 1.8 1.8];\n", +"fs=Sin-fp;\n", +"L3=4*Sout./(fs-fp);L=0;\n", +"for i=1:length(L3)\n", +" L=L+L3(i);\n", +"end\n", +"disp('By using 2 stage interpolator filter length is:')\n", +"ceil(L)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.8: Design_of_Interpolating_Filters.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Design of interpolating filters\n", +"//(a)Design using a single stage interpolator\n", +"fp=1.8;Sout=48;Sin=4;\n", +"Ap=0.6;As=50;\n", +"fs=Sin-fp;\n", +"//finding ripple parameters\n", +"delp=(10^(Ap/20)-1)/(10^(Ap/20)+1);\n", +"dels=10^(-As/20);\n", +"N=Sout*(-10*log10(delp*dels)-13)/(14.6*(fs-fp))+1;\n", +"disp('By using single stage interpolator the filter design is:');\n", +"ceil(N)\n", +"//Design using 3-stage interpolator with I1=2;I2=3;I3=2\n", +"Ap=0.2;\n", +"Sin=[4 8 24];\n", +"Sout=[8 24 48];\n", +"fp=[1.8 1.8 1.8];\n", +"fs=Sin-fp;\n", +"delp=(10^(Ap/20)-1)/(10^(Ap/20)+1);\n", +"dels=10^(-As/20);\n", +"p=14.6*(fs-fp);\n", +"N1=((-10*log10(delp*dels)-13)./p);\n", +"N1=(Sout.*N1)+1;N=0;\n", +"for i=1:length(N1)\n", +" N=N+N1(i);\n", +"end\n", +"disp('By using single stage interpolator the filter design is:');\n", +"ceil(N)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.9: Multistage_Decimatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//The concept of multistage Decimation\n", +"//(a)Single stage decimator\n", +"Sin=48;Sout=4;\n", +"fp=1.8;\n", +"fs=Sout-fp;\n", +"FT=(fs-fp)/Sin;\n", +"disp('By using single stage the total filter length is:') \n", +"L=4/FT\n", +"//(b)Two-stage decimator\n", +"Sin=[48 12];\n", +"D=[4 3];//decimating factors\n", +"Sout=[12 4];\n", +"fp=[1.8 1.8];\n", +"fs=Sout-fp;\n", +"L1=4*Sin./(fs-fp);\n", +"L=0;\n", +"for i=1:length(L1)\n", +" L=L+L1(i);\n", +"end\n", +"disp('By using 2 stage decimator filter length is:')\n", +"ceil(L)\n", +"//3 stage decimator with D1=2;D2=3;D3=2\n", +"Sin=[48 24 8];\n", +"D=[2 3 2];\n", +"Sout=[24 8 4];\n", +"fp=[1.8 1.8 1.8];\n", +"fs=Sout-fp;\n", +"L2=4*Sin./(fs-fp);L=0;\n", +"for i=1:length(L2)\n", +" L=L+L2(i);\n", +"end\n", +"disp('By using 3 stage decimator filter length is:')\n", +"ceil(L)\n", +"//3 stage decimator with I1=2;I2=3;I3=2\n", +"Sin=[48 24 12];\n", +"D=[2 2 3];\n", +"Sout=[24 12 4];\n", +"fp=[1.8 1.8 1.8];\n", +"fs=Sout-fp;\n", +"L3=4*Sin./(fs-fp);L=0;\n", +"for i=1:length(L3)\n", +" L=L+L3(i);\n", +"end\n", +"disp('By using 3 stage decimator filter length is:')\n", +"ceil(L)" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/2-Discrete_Signals.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/2-Discrete_Signals.ipynb new file mode 100644 index 0000000..36b5000 --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/2-Discrete_Signals.ipynb @@ -0,0 +1,486 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 2: Discrete Signals" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.1a: Signal_energy_and_power.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.1.a,pg no.11\n", +"for i=1:1:50\n", +" x(1,i)=3*(0.5)^(i-1);\n", +"end\n", +"//summation of x\n", +"E=0\n", +"for i=1:1:50\n", +" E=E+x(1,i)^2;\n", +"end\n", +"disp('the energy of given signal is')\n", +"E" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.1b: Average_power_of_periodic_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.1b,pg.no.11\n", +"n=1:1:10;\n", +"xn=6*cos((2*%pi*n')/4);\n", +"a=4;\n", +"p=0;\n", +"for i=1:1:a\n", +" p=p+abs(xn(i)^2);\n", +"end\n", +"P=p/a;\n", +"disp('The average power of given signal is')\n", +"P" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.1c: Average_power_of_periodic_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.1c,pg.no.11\n", +"n=1:4;\n", +"xn=6*%e^((%i*%pi*n')/2);\n", +"a=4;\n", +"p=0;\n", +"for i=1:1:a\n", +" p=p+abs(xn(i)^2);\n", +"end\n", +"P=p/a;\n", +"disp('The average power of given signal is')\n", +"P" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.2: Operations_on_Discrete_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.2,pg no.12\n", +"x=[0 2 3 4 5 6 7];\n", +"n1=-3:1:3;\n", +"y=x;\n", +"n2=0:1:6;\n", +"f=x;\n", +"n3=-5:1:1;\n", +"g=x(length(x):-1:1);\n", +"n4=-3:1:3;\n", +"h=x(length(x):-1:1);\n", +"n5=-2:1:4;\n", +"s=x(length(x):-1:1);\n", +"n6=-5:1:1;\n", +"a=gca();\n", +"subplot(231);\n", +"plot2d3('gnn',n1,x);\n", +"ylabel('x[n]');\n", +"subplot(232);\n", +"plot2d3('gnn',n2,y);\n", +"ylabel('y[n]=x[n-3]');\n", +"subplot(233);\n", +"plot2d3('gnn',n3,f);\n", +"ylabel('f[n]=x[n+2]');\n", +"subplot(234);\n", +"plot2d3('gnn',n4,g);\n", +"ylabel('g[n]=x[-n]');\n", +"subplot(235);\n", +"plot2d3('gnn',n5,h);\n", +"ylabel('h[n]=x[-n+1]');\n", +"subplot(236);\n", +"plot2d3('gnn',n6,s);\n", +"ylabel('s[n]=x[-n-2]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.3a: Even_and_Odd_parts_of_Discrete_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.3a pg.no.14\n", +"clear;clc;close;\n", +"n=-2:2;\n", +"x1=[4 -2 4 -6 0];\n", +"x2=0.5*x1//x[n]\n", +"x3=0.5*[x1(length(x1):-1:1)];//x[-n]\n", +"xe=(x2+x3);//even part\n", +"xo=(x2-x3);//odd part\n", +"a=gca();\n", +"a.thickness=2;\n", +"a.x_location='middle';\n", +"a.y_location='middle';\n", +"plot2d3('gnn',n,xe,rect=[-4 -6 4 6])\n", +"xtitle('graphical representation of even part of x[n]','n','x[n]')\n", +"xset('window',1)\n", +"b=gca();\n", +"b.thickness=2;\n", +"b.y_location='middle';\n", +"b.x_location='middle';\n", +"plot2d3('gnn',n,xo,rect=[-2 -4 2 4])\n", +"xtitle('graphical representation of odd part of x[n]','n','x[n]')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.3b: Even_and_Odd_parts_of_Discrete_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.3a pg.no.14\n", +"clear;clc;close;\n", +"x1=[0 0 0 0 0 1 1 1 1 ];\n", +"n=-4:4;\n", +"x2=0.5*x1//x[n]\n", +"x3=0.5*[x1(length(x1):-1:1)]//x[-n]\n", +"xe=(x2+x3);//even part\n", +"xo=(x2-x3);//odd part\n", +"a=gca();\n", +"a.thickness=2;\n", +"a.y_location='middle';\n", +"a.x_location='middle';\n", +"plot2d3('gnn',n,xe,rect=[-4 -1 4 1]);\n", +"xtitle('graphical representation of even part of x[n]','n','x[n]')\n", +"xset('window',1)\n", +"b=gca();\n", +"b.thickness=2;\n", +"b.y_location='middle';\n", +"b.x_location='middle';\n", +"plot2d3('gnn',n,xo,rect=[-4 -1 4 1]);\n", +"xtitle('graphical representation of odd part of x[n]','n','x[n]')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.4a: Decimation_and_Interpolation_of_Discrete_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.4a pg.no.17\n", +"x=[1 2 5 -1];\n", +"xm=2;//denotes 2nd sample has pad.\n", +"y=[x(1:2:xm-2),x(xm:2:length(x))]//decimation\n", +"h=[x(1:1/3:length(x))]//step interpolated\n", +"g=h;\n", +"for i=2:3\n", +" g(i:3:length(g))=0;\n", +"end\n", +"//zero interpolated\n", +"x1=1:3:3*length(x);\n", +"s=interpln([x1;x],1:10)//linear interpolated" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.4b: Decimation_and_Interpolation_of_Discrete_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.4 b,c.pg.no.17\n", +"x=[3 4 5 6];\n", +"xm=3;//denotes 3rd sample has pad\n", +"xm=xm-1;//shifting\n", +"g=[x(xm-2:-2:1),x(xm:2:length(x))]//decimation\n", +"xm=3;\n", +"h=[x(1:1/2:length(x))]//step interpolated" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.4c: Decimation_and_Interpolation_of_Discrete_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.4c,pg.no.17\n", +"x=[3 4 5 6];\n", +"xm=3;\n", +"xm=xm+1*(xm-1);//shift in pad due to interpolation\n", +"xm=xm-2//normal shifting\n", +"x1=[x(1:1/3:length(x))]//step interpolated\n", +"xm=3;\n", +"xm=xm+2*(xm-1)//shift in pad due to interpolation\n", +"y=[x1(1:2:xm-2),x1(xm:2:length(x1))]//decimation" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.4d: Decimation_and_Interpolation_of_Discrete_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.4d,pg.no.17\n", +"x=[2 4 6 8]\n", +"xm=3;//denote 3rd sample has pad\n", +"x1=[1 3 5 7]\n", +"x2=interpln([x1;x],1:6)\n", +"xm=xm+1*(xm-1);//shift in pad due to interpolation\n", +"xm=xm-1//shift in pad due to delay\n", +"y=[x2(2:2:xm-2),x2(xm:2:length(x2))]//decimation" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.5: Describing_Sequences_and_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.5,pg.no.20\n", +"x=[1 2 4 8 16 32 64];\n", +"y=[0 0 0 1 0 0 0];\n", +"z=x.*y;\n", +"a=0;\n", +"for i=1:length(z)\n", +" a=a+z(i);\n", +"end\n", +"z,a//a=summation of z" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.6: Discrete_time_Harmonics_and_Periodicity.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.6 pg.no.23\n", +"function[p]=period(x)\n", +"for i=2:length(x)\n", +" v=i\n", +" if (abs(x(i)-x(1))<0.00001) \n", +" k=2\n", +" for j=i+1:i+i\n", +" if (abs(x(j)-x(k))<0.00001) \n", +" v=v+1\n", +" end\n", +" k=k+1; \n", +" end\n", +" end \n", +" if (v==(2*i)) then\n", +" break\n", +" end\n", +"end\n", +"p=i-1\n", +"endfunction\n", +"for i=1:60\n", +" x1(i)=cos((2*%pi*8*i)/25);\n", +"end\n", +"for i=1:60\n", +" x2(i)=exp(%i*0.2*i*%pi)+exp(-%i*0.3*i*%pi);\n", +"end\n", +"for i=1:45\n", +" x3(i)=2*cos((40*%pi*i)/75)+sin((60*%pi*i)/75);\n", +"end\n", +"period(x1)\n", +"period(x2)\n", +"period(x3)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.7: Aliasing_and_its_effects.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//example 2.7.pg.no.27\n", +"f=100;\n", +"s=240;\n", +"s1=s;\n", +"aliasfrequency(f,s)\n", +"s=140;\n", +"s1=s;\n", +"aliasfrequency(f,s,s1)\n", +"s=90;\n", +"s1=s;\n", +"aliasfrequency(f,s,s1)\n", +"s=35;\n", +"s1=s;\n", +"aliasfrequency(f,s,s1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.8: Signal_Reconstruction.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"f=100;\n", +"s=210;\n", +"s1=420;\n", +"aliasfrequency(f,s,s1)\n", +"s=140;\n", +"aliasfrequency(f,s,s1)" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/3-Response_of_Digital_Filters.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/3-Response_of_Digital_Filters.ipynb new file mode 100644 index 0000000..c3ee5f6 --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/3-Response_of_Digital_Filters.ipynb @@ -0,0 +1,776 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 3: Response of Digital Filters" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.19a: Analytical_Evaluation_of_Discrete_Convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Analytical evaluation of Discrete Convolution\n", +"clear;close;clc;\n", +"max_limit=10;\n", +"h=ones(1,max_limit);\n", +"n2=0:length(h)-1;\n", +"x=h;\n", +"n1=-length(x)+1:0;\n", +"y=convol(x,h);\n", +"n=-length(x)+1:length(h)-1;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h)\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x)\n", +"a.y_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"xtitle('output response','n','y[n]');\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.19b: Analytical_Evaluation_of_Discrete_Convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear;close;clc;\n", +"max_limit=10;\n", +"for n=1:max_limit\n", +" h(n)=(0.4)^n;\n", +"end\n", +"n2=0:length(h)-1;\n", +"for n=1:max_limit\n", +" x(n)=(0.8)^n;\n", +"end\n", +"n1=-length(x)+1:0;\n", +"y=convol(x,h)\n", +"n=-length(x)+1:length(h)-1;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h)\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x)\n", +"a.y_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"xtitle('output response','n','y[n]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.19c: Analytical_Evaluation_of_Discrete_Convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Analytical Evaluation of Discrete convolution\n", +"clear;close;clc;\n", +"max_limit=5;\n", +"h(1)=0;\n", +"for n=2:max_limit\n", +" h(n)=0.8^n;\n", +"end\n", +"n2=0:length(h)-1;\n", +"x=[0 ones(1:max_limit)]\n", +"n1=-length(x)+1:0;\n", +"y=convol(x,h);\n", +"n=-length(x)+1:length(h)-1;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h)\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"a=gca();\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x)\n", +"a.y_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"xtitle('output response','n','y[n]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.20a: Properties_of_Convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//properties of convolution\n", +"x=[1 2 3 4 5];\n", +"h=[1 zeros(1:5)];\n", +"a=convol(x,h);\n", +"b=convol(h,x);\n", +"a==b" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.20b: Properties_of_Convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Convolution with Step Function\n", +"x=[1 2 3 4 5];\n", +"h=[ones(1:5)];\n", +"a=convol(h,x);\n", +"b(1)=a(1);\n", +"for i=2:length(x)\n", +" b(i)=b(i-1)+x(i);\n", +"end\n", +"disp(a(1:length(x)),b,'Step Response is running sum of impulses can be seen below');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.21a: Convolution_of_finite_length_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//convolution of finite length signals\n", +"clear;close;clc;\n", +"max_limit=10;\n", +"h=[1 2 2 3];\n", +"n2=0:length(h)-1;\n", +"x=[2 -1 3];\n", +"n1=0:length(x)-1;\n", +"y=convol(x,h);\n", +"n=0:length(h)+length(x)-2;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h)\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"a=gca();\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x)\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',2);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"a.y_location='origin';\n", +"xtitle('output response','n','y[n]');\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.21b: Convolution_of_finite_length_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear;close;clc;\n", +"max_limit=10;\n", +"h=[2 5 0 4];\n", +"n2=-2:length(h)-3;\n", +"x=[4 1 3];\n", +"n1=-1:length(x)-2;\n", +"y=convol(x,h);\n", +"n=-3:length(x)+length(h)-5;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h)\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"a=gca();\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x)\n", +"a.y_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"xtitle('output response','n','y[n]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.21c: Convolution_of_finite_length_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear;close;clc;\n", +"max_limit=10;\n", +"h=[1/2 1/2 1/2];\n", +"n2=0:length(h)-1;\n", +"x=[2 4 6 8 10];\n", +"n1=0:length(x)-1;\n", +"y=convol(x,h);\n", +"n=0:length(x)+length(h)-2;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h);\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"a=gca();\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x);\n", +"a.y_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"xtitle('output response','n','y[n]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.22: Convolution_of_finite_length_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"max_limit=10;\n", +"h=[2 5 0 4];\n", +"n2=0:length(h)-1;\n", +"x=[4 1 3];\n", +"n1=0:length(x)-1;\n", +"y=convol(x,h);\n", +"n=0:length(x)+length(h)-2;\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d3('gnn',n2,h)\n", +"xtitle('impulse Response','n','h[n]');\n", +"a.thickness=2;\n", +"a.y_location='origin';\n", +"a=gca();\n", +"subplot(212);\n", +"plot2d3('gnn',n1,x)\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"xtitle('input response','n','x[n]');\n", +"xset('window',1);\n", +"a=gca();\n", +"plot2d3('gnn',n,y)\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"xtitle('output response','n','y[n]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.23: effect_of_Zero_Insertion.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//convolution by polynomial method\n", +"x=[4 1 3];\n", +"h=[2 5 0 4];\n", +"z=%z;\n", +"n=length(x)-1:-1:0;\n", +"X=x*z^n';\n", +"n1=length(h)-1:-1:0;\n", +"H=h*z^n1';\n", +"y=X*H\n", +"//effect of zero insertion on convolution\n", +"h=[2 0 5 0 0 0 4];\n", +"x=[4 0 1 0 3];\n", +"y=convol(x,h)\n", +"//effect of zero padding on convolution\n", +"h=[2 5 0 4 0 0];\n", +"x=[4 1 3 0];\n", +"y=convol(x,h)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.25: Stability_and_Causality.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//concepts based on stability and Causality\n", +"function[]=stability(X)\n", +"if (abs(roots(X))<1)\n", +" disp('given system is stable')\n", +"else \n", +" disp('given system is not stable')\n", +"end\n", +"endfunction\n", +"x=[1 -1/6 -1/6];\n", +"z=%z;\n", +"n=length(x)-1:-1:0;\n", +"//characteristic eqn is\n", +"X=x*(z)^n'\n", +"stability(X)\n", +"x=[1 -1];\n", +"n=length(x)-1:-1:0;\n", +"//characteristic eqn is\n", +"X=x*(z)^n'\n", +"stability(X)\n", +"x=[1 -2 1];\n", +"n=length(x)-1:-1:0;\n", +"//characteristic eqn is\n", +"X=x*(z)^n'\n", +"stability(X)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.26: Response_to_Periodic_Inputs.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Response of periodic inputs\n", +"function[p]=period(x)\n", +"for i=2:length(x)\n", +" v=i\n", +" if (abs(x(i)-x(1))<0.00001) \n", +" k=2\n", +" for j=i+1:i+i\n", +" if (abs(x(j)-x(k))<0.00001) \n", +" v=v+1\n", +" end\n", +" k=k+1; \n", +" end\n", +" end \n", +" if (v==(2*i)) then\n", +" break\n", +" end\n", +"end\n", +"p=i-1\n", +"endfunction\n", +"x=[1 2 -3 1 2 -3 1 2 -3];\n", +"h=[1 1];\n", +"y=convol(x,h)\n", +"y(1)=y(4);\n", +"period(x)\n", +"period(y)\n", +"h=[1 1 1];\n", +"y=convol(x,h)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.27: Periodic_Extension.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//to find periodic extension\n", +"x=[1 5 2;0 4 3;6 7 0];\n", +"y=[0 0 0];\n", +"for i=1:3\n", +" for j=1:3\n", +" y(i)=y(i)+x(j,i);\n", +" end\n", +"end\n", +"y\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.28: System_Response_to_Periodic_Inputs.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//method of wrapping to fing convolution of periodic signal with one period\n", +"x=[1 2 -3];\n", +"h=[1 1];\n", +"y1=convol(h,x)\n", +"y1=[y1,zeros(5:9)]\n", +"y2=[y1(1:3);y1(4:6);y1(7:9)];\n", +"y=[0 0 0];\n", +"for i=1:3\n", +" for j=1:3\n", +" y(i)=y(i)+y2(j,i);\n", +" end\n", +"end\n", +"y\n", +"x=[2 1 3];\n", +"h=[2 1 1 3 1];\n", +"y1=convol(h,x)\n", +"y1=[y1,zeros(8:9)]\n", +"y2=[y1(1:3);y1(4:6);y1(7:9)];\n", +"y=[0 0 0];\n", +"for i=1:3\n", +" for j=1:3\n", +" y(i)=y(i)+y2(j,i);\n", +" end\n", +"end\n", +"y" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.29: Periodic_Convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//periodic or circular convolution\n", +"x=[1 0 1 1];\n", +"h=[1 2 3 1];\n", +"y1=convol(h,x)\n", +"y1=[y1,zeros(8:12)];\n", +"y2=[y1(1:4);y1(5:8);y1(9:12)];\n", +"y=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:3\n", +" y(i)=y(i)+y2(j,i);\n", +" end\n", +"end\n", +"y" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.30: Periodic_Convolution_by_Circulant_Matrix.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//periodic convolution by circulant matrix\n", +"x=[1 0 2];\n", +"h=[1;2;3];\n", +"//generation of circulant matrix\n", +"c(1,:)=x;\n", +"for i=2:length(x)\n", +" c(i,:)=[x(length(x):length(x)-i),x(1:length(x)-i)]\n", +"end\n", +"c'" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.32: Deconvolution_By_polynomial_Division.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//deconvolution by polynomial division\n", +"x=[2 5 0 4];\n", +"y=[8 22 11 31 4 12];\n", +"z=%z\n", +"n=length(x)-1:-1:0;\n", +"X=x*(z)^n'\n", +"n1=length(y)-1:-1:0;\n", +"Y=y*(z)^n1'\n", +"h=Y/X" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.33: Autocorrelation_and_Cross_Correlation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//discrete auto correlation and cross correlation\n", +"x=[2 5 0 4];\n", +"h=[3 1 4];\n", +"x1=x(length(x):-1:1)\n", +"h1=h(length(h):-1:1)\n", +"rxhn=convol(x,h1)\n", +"rhxn=convol(x1,h)\n", +"rhxn1=rhxn(length(rhxn):-1:1)\n", +"//we observe that rhxn1=rxhn\n", +"x=[3 1 -4];\n", +"x1=x(length(x):-1:1)\n", +"rxxn=convol(x,x1)\n", +"//we observe that rxxn is even symmetric about origin" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.35: Periodic_Autocorrelation_and_Cross_Correlation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//discrete periodic auto correlation and cross correlation\n", +"x=[2 5 0 4];\n", +"h=[3 1 -1 2];\n", +"x1=x(length(x):-1:1);\n", +"h1=h(length(h):-1:1);\n", +"rxhn=convol(x,h1)\n", +"rhxn=convol(x1,h)\n", +"rxxn=convol(x,x1)\n", +"rhhn=convol(h,h1)\n", +"y1=[rxhn,zeros(8:12)];\n", +"y2=[y1(1:4);y1(5:8);y1(9:12)];\n", +"y3=[rhxn,zeros(8:12)];\n", +"y4=[y3(1:4);y3(5:8);y3(9:12)];\n", +"y5=[rxxn,zeros(8:12)];\n", +"y6=[y5(1:4);y5(5:8);y5(9:12)];\n", +"y7=[rhhn,zeros(8:12)];\n", +"y8=[y7(1:4);y7(5:8);y7(9:12)];\n", +"rxhp=[0 0 0 0];\n", +"rhxp=[0 0 0 0];\n", +"rxxn=[0 0 0 0];\n", +"rhhp=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:3\n", +" rhxp(i)=rhxp(i)+y4(j,i);\n", +" rxhp(i)=rxhp(i)+y2(j,i);\n", +" rxxn(i)=rxxn(i)+y6(j,i);\n", +" rhhp(i)=rhhp(i)+y8(j,i);\n", +" end\n", +"end\n", +"rxhp\n", +"rhxp\n", +"rxxn\n", +"rhhp" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.5: FIR_filter_response.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Response of non-recursive Filters\n", +"for i=1:4\n", +" x(i)=0.5^i;\n", +"end\n", +"x1=[0;1;x(1:2)]\n", +"for i=1:4\n", +" y(i)=2*x(i)-3*x1(i);\n", +"end \n", +"y(1),y(2)" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/4-z_Transform_Analysis.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/4-z_Transform_Analysis.ipynb new file mode 100644 index 0000000..13f9d3d --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/4-z_Transform_Analysis.ipynb @@ -0,0 +1,290 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 4: z Transform Analysis" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.10: Inverse_Transform_of_sequences.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//inverse transform of sequences\n", +"//(a)X(z)=3z^-1+5z^-3+2z^-4\n", +"z=%z;\n", +"X1=[3*z^-1;0;5*z^-3;2*z^-4];\n", +"n1=1:4;\n", +"ZI=z^n1';\n", +"x1=numer(X1.*ZI);\n", +"disp(x1,'x[n]=');\n", +"//(b)X(z)=2z^2-5z+5z^-1-2z^-2\n", +"X2=[2*z^2;-5*z;0;5*z^-1;-2*z^-2];\n", +"n2=-2:2;\n", +"ZI=z^n2';\n", +"x2=numer(X2.*ZI);\n", +"disp(x2,'x[n]=');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.11: Inverse_Transform_by_Long_Division.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//inverse transform by long division\n", +"z=%z;\n", +"x=ldiv(z-4,1-z+z^2,5)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.12: Inverse_transform_of_Right_sided_sequences.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//inverse z transforms from standard transforms\n", +"z=%z;\n", +"xz=z/((z-0.5)*(z-0.25));\n", +"yz=xz/z;\n", +"pfss(yz)\n", +"//hence x[n]=-4(0.25)^n*un+4(0.5)^n*un;\n", +"xz=1/((z-0.5)*(z-0.25));\n", +"yz=xz/z;\n", +"pfss(yz)\n", +"//hence x[n]=-4(0.25)^n-1*u[n-1]+4(0.5)^n-1*u[n-1];\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.1b: z_transform_of_finite_length_sequences.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"function[za]=ztransfer(sequence,n)\n", +" z=poly(0,'z','r')\n", +" za=sequence*(1/z)^n'\n", +"endfunction\n", +"x1=[2 1 -5 4];\n", +"n=-1:length(x1)-2;\n", +"ztransfer(x1,n) " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.20: z_Transform_of_Switched_periodic_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//z transform of switched periodic signals\n", +"z=%z;\n", +"//sol. for 4.20a\n", +"x1=[0 1 2];\n", +"n=0:2;\n", +"N=3;\n", +"x1z=x1*(1/z)^n'\n", +"xz=x1z/(1-z^-N)\n", +"//sol.for 4.20b\n", +"x1=[0 1 0 -1];\n", +"n=0:3;\n", +"N=4;\n", +"x1z=x1*(1/z)^n'\n", +"xz=x1z/(1-z^-N)\n", +"//sol.for 4.20c\n", +"xz=(2+z^-1)/(1-z^-3);\n", +"x1z=numer(xz)\n", +"//thus first period of xn is [2 1 0]\n", +"//sol.for 4.20d\n", +"xz=(z^-1-z^-4)/(1-z^-6);\n", +"x1z=numer(xz)\n", +"//thus first period of xn is [0 1 0 0 -1 0]" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.4a: Pole_Zero_Plots.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Pole-Zero plots\n", +"z=%z;\n", +"az=2*z*(z+1);\n", +"bz=(z-1/3)*((z^2)+1/4)*((z^2)+4*z+5);\n", +"poles=roots(bz)\n", +"zeroes=roots(az)\n", +"h=az/bz\n", +"plzr(h)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.4b: Pole_Zero_plots.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Pole-Zero plots\n", +"z=%z;\n", +"az=z^4+4.25*z^2+1;\n", +"bz=z^4;\n", +"poles=roots(bz)\n", +"zeroes=roots(az)\n", +"h=az/bz\n", +"plzr(h)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.8: Stability_of_Recursive_Filters.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//stability of recursive filter\n", +"//for roc:/z/>/a/ \n", +"a=input('enter the value of alpha')\n", +"z=%z;\n", +"H=z/(z-a);\n", +"if (abs(a)<1)\n", +" disp('system is stable')\n", +"else\n", +" disp('system is not stable')\n", +" end\n", +" //for roc:/z/</a/\n", +" if (abs(a)>1)\n", +" disp('system is stable')\n", +"else\n", +" disp('system is not stable')\n", +" end\n", +" " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.9: Inverse_Systems.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//inverse systems\n", +"z=%z;\n", +"H=(1+2*(z^(-1)))/(1+3*(z^(-1)));\n", +"//inverse of H is\n", +"H1=1/H\n", +"H=1+2*(z^(-1))+3*(z^(-2));\n", +"H1=1/H" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/5-Frequency_Domain_Analysis.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/5-Frequency_Domain_Analysis.ipynb new file mode 100644 index 0000000..e5ef6e2 --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/5-Frequency_Domain_Analysis.ipynb @@ -0,0 +1,742 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 5: Frequency Domain Analysis" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.10a: System_Representation_in_various_forms.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//System Representation in various forms\n", +"a=0.8;b=2;\n", +"n=0:50;\n", +"h=b*(a^n);\n", +"//Discrete-Time Fourier transform\n", +"K=500;\n", +"k=0:1:K;\n", +"w=%pi*k/K;\n", +"H=h*exp(-%i*n'*w);\n", +"//caluculation of phase and magnitude of h(z)\n", +"[phase_H,m]=phasemag(H);\n", +"H=abs(H);\n", +"subplot(2,1,1);\n", +"plot2d(w/%pi,H);\n", +"xlabel('Frequency in radians')\n", +"ylabel('abs(H)')\n", +"title('magnitude Response')\n", +"subplot(2,1,2)\n", +"plot2d(w/%pi,phase_H)\n", +"xlabel('Frequency in Radians');\n", +"ylabel('<(H)')\n", +"title('Phase Response')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.10b: System_Representation_in_various_forms.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//System Representation in various forms\n", +"a=0.6;b=1;\n", +"n=0:50;\n", +"h=b*(a^n);\n", +"//Discrete-Time Fourier transform\n", +"K=500;\n", +"k=0:1:K;\n", +"w=%pi*k/K;\n", +"H=h*exp(-%i*n'*w);\n", +"//caluculation of phase and magnitude of h(z)\n", +"[phase_H,m]=phasemag(H);\n", +"H=abs(H);\n", +"subplot(2,1,1);\n", +"plot2d(w/%pi,H);\n", +"xlabel('Frequency in radians')\n", +"ylabel('abs(H)')\n", +"title('magnitude Response')\n", +"subplot(2,1,2)\n", +"plot2d(w/%pi,phase_H)\n", +"xlabel('Frequency in Radians');\n", +"ylabel('<(H)')\n", +"title('Phase Response')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.1c: DTFT_from_Defining_Relation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT of x[n]=(a)^n*u[n]\n", +"clear;\n", +"clc;close;\n", +"//DTS signal\n", +"a1=0.5;\n", +"a2=-0.5;\n", +"max_limit=10;\n", +"for n=0:max_limit-1\n", +" x1(n+1)=(a1^n);\n", +" x2(n+1)=(a2^n);\n", +"end\n", +"n=0:max_limit-1;\n", +"//discrete time fourier transform\n", +"wmax=2*%pi;\n", +"K=4;\n", +"k=0:(K/1000):K;\n", +"W=k*wmax/K;\n", +"x1=x1'\n", +"x2=x2'\n", +"XW1=x1*exp(%i*n'*W);\n", +"XW2=x2*exp(%i*n'*W);\n", +"XW1_Mag=abs(XW1);\n", +"XW2_Mag=abs(XW2);\n", +"W=[-mtlb_fliplr(W),W(2:1001)];//omega form\n", +"XW1_Mag=[mtlb_fliplr(XW1_Mag),XW1_Mag(2:1001)];\n", +"XW2_Mag=[mtlb_fliplr(XW2_Mag),XW2_Mag(2:1001)];\n", +"[XW1_phase,db]=phasemag(XW1);\n", +"[XW2_phase,db]=phasemag(XW2);\n", +"XW1_phase=[-mtlb_fliplr(XW1_phase),XW1_phase(2:1001)];\n", +"XW2_phase=[-mtlb_fliplr(XW2_phase),XW2_phase(2:1001)];\n", +"\n", +"//plot for a>0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x1)\n", +"xtitle('Discrete time sequencex[n] a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d3(W,XW1_Mag);\n", +"title('magnitude Response abs(exp(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW1_phase);\n", +"title('magnitude Response abs(exp(jw))')\n", +"//plot for a<0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x2);\n", +"xtitle('Discrete Time sequence x[n] for a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_Mag);\n", +"title('Magnitude Response abs(X(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_phase);\n", +"title('phase Response<(X(jw))')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.3a: Some_DTFT_pairs_using_properties.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT of x[n]=n*(a)^n*u[n]\n", +"clear;\n", +"clc;close;\n", +"//DTS signal\n", +"a1=0.5;\n", +"a2=-0.5;\n", +"max_limit=10;\n", +"for n=0:max_limit-1\n", +" x1(n+1)=n*(a1^n);\n", +" x2(n+1)=n*(a2^n);\n", +"end\n", +"n=0:max_limit-1;\n", +"//discrete time fourier transform\n", +"wmax=2*%pi;\n", +"K=4;\n", +"k=0:(K/1000):K;\n", +"W=k*wmax/K;\n", +"x1=x1';\n", +"x2=x2';\n", +"XW1=x1*exp(%i*n'*W);\n", +"XW2=x2*exp(%i*n'*W);\n", +"XW1_Mag=abs(XW1);\n", +"XW2_Mag=abs(XW2);\n", +"W=[-mtlb_fliplr(W),W(2:1001)];//omega form\n", +"XW1_Mag=[mtlb_fliplr(XW1_Mag),XW1_Mag(2:1001)];\n", +"XW2_Mag=[mtlb_fliplr(XW2_Mag),XW2_Mag(2:1001)];\n", +"[XW1_phase,db]=phasemag(XW1);\n", +"[XW2_phase,db]=phasemag(XW2);\n", +"XW1_phase=[-mtlb_fliplr(XW1_phase),XW1_phase(2:1001)];\n", +"XW2_phase=[-mtlb_fliplr(XW2_phase),XW2_phase(2:1001)];\n", +"\n", +"//plot for a>0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x1)\n", +"xtitle('Discrete time sequencex[n] a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d3(W,XW1_Mag);\n", +"title('magnitude Response abs(exp(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW1_phase);\n", +"title('magnitude Response abs(exp(jw))')\n", +"//plot for a<0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x2);\n", +"xtitle('Discrete Time sequence x[n] for a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_Mag);\n", +"title('Magnitude Response abs(X(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_phase);\n", +"title('phase Response<(X(jw))')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.3b: Some_DTFT_pairs_using_properties.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT of x[n]=n*(a)^n*u[n]\n", +"clear;\n", +"clc;close;\n", +"//DTS signal\n", +"a1=0.5;\n", +"a2=-0.5;\n", +"max_limit=10;\n", +"for n=0:max_limit-1\n", +" x1(n+1)=(n+1)*(a1^n);\n", +" x2(n+1)=(n+1)*(a2^n);\n", +"end\n", +"n=0:max_limit-1;\n", +"//discrete time fourier transform\n", +"wmax=2*%pi;\n", +"K=4;\n", +"k=0:(K/1000):K;\n", +"W=k*wmax/K;\n", +"x1=x1';\n", +"x2=x2';\n", +"XW1=x1*exp(%i*n'*W);\n", +"XW2=x2*exp(%i*n'*W);\n", +"XW1_Mag=abs(XW1);\n", +"XW2_Mag=abs(XW2);\n", +"W=[-mtlb_fliplr(W),W(2:1001)];//omega form\n", +"XW1_Mag=[mtlb_fliplr(XW1_Mag),XW1_Mag(2:1001)];\n", +"XW2_Mag=[mtlb_fliplr(XW2_Mag),XW2_Mag(2:1001)];\n", +"[XW1_phase,db]=phasemag(XW1);\n", +"[XW2_phase,db]=phasemag(XW2);\n", +"XW1_phase=[-mtlb_fliplr(XW1_phase),XW1_phase(2:1001)];\n", +"XW2_phase=[-mtlb_fliplr(XW2_phase),XW2_phase(2:1001)];\n", +"\n", +"//plot for a>0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x1)\n", +"xtitle('Discrete time sequencex[n] a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d3(W,XW1_Mag);\n", +"title('magnitude Response abs(exp(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW1_phase);\n", +"title('magnitude Response abs(exp(jw))')\n", +"//plot for a<0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x2);\n", +"xtitle('Discrete Time sequence x[n] for a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_Mag);\n", +"title('Magnitude Response abs(X(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_phase);\n", +"title('phase Response<(X(jw))')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.3d: Some_DTFT_pairs_using_properties.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT of x[n]=a^abs(n)\n", +"a=0.5;\n", +"n=-9:9;\n", +"x=a^abs(n);\n", +"//Discrete time Fourier Transform\n", +"k=0:(4/1000):4;\n", +"w=(2*%pi*k)/4;\n", +"xw=x*exp(%i*n'*w);\n", +"xw_mag=real(xw);\n", +"w=[-mtlb_fliplr(xw_mag),w(2:1001)];\n", +"xw_mag=[mtlb_fliplr(xw_mag),xw_mag(2:1001)];\n", +"figure\n", +"subplot(2,1,1);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d3('gnn',n,x);\n", +"xtitle('discrete time sequence x[n]');\n", +"subplot(2,1,2);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(w,xw_mag);\n", +"title('discrete time fourier transform x(exp(jw))');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.3e: Some_DTFT_pairs_using_properties.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT of x[n]=n*(a)^n*u[n]\n", +"clear;\n", +"clc;close;\n", +"//DTS signal\n", +"a1=0.5;\n", +"a2=-0.5;\n", +"max_limit=10;\n", +"for n=0:max_limit-1\n", +" x1(n+1)=4*(a1^(n+3));\n", +" x2(n+1)=4*(a2^(n+3));\n", +"end\n", +"n=0:max_limit-1;\n", +"//discrete time fourier transform\n", +"wmax=2*%pi;\n", +"K=4;\n", +"k=0:(K/1000):K;\n", +"W=k*wmax/K;\n", +"x1=x1';\n", +"x2=x2';\n", +"XW1=x1*exp(%i*n'*W);\n", +"XW2=x2*exp(%i*n'*W);\n", +"XW1_Mag=abs(XW1);\n", +"XW2_Mag=abs(XW2);\n", +"W=[-mtlb_fliplr(W),W(2:1001)];//omega form\n", +"XW1_Mag=[mtlb_fliplr(XW1_Mag),XW1_Mag(2:1001)];\n", +"XW2_Mag=[mtlb_fliplr(XW2_Mag),XW2_Mag(2:1001)];\n", +"[XW1_phase,db]=phasemag(XW1);\n", +"[XW2_phase,db]=phasemag(XW2);\n", +"XW1_phase=[-mtlb_fliplr(XW1_phase),XW1_phase(2:1001)];\n", +"XW2_phase=[-mtlb_fliplr(XW2_phase),XW2_phase(2:1001)];\n", +"\n", +"//plot for a>0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x1)\n", +"xtitle('Discrete time sequencex[n] a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d3(W,XW1_Mag);\n", +"title('magnitude Response abs(exp(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW1_phase);\n", +"title('magnitude Response abs(exp(jw))')\n", +"//plot for a<0\n", +"figure\n", +"subplot(3,1,1);\n", +"plot2d3('gnn',n,x2);\n", +"xtitle('Discrete Time sequence x[n] for a>0')\n", +"subplot(3,1,2);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_Mag);\n", +"title('Magnitude Response abs(X(jw))')\n", +"subplot(3,1,3);\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d(W,XW2_phase);\n", +"title('phase Response<(X(jw))')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.4: DTFT_of_periodic_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTfT of periodic signals\n", +"x=[3 2 1 2];//one period of signal\n", +"n=0:3;\n", +"k=0:3;\n", +"x1=x*exp(%i*n'*2*k*%pi/4)\n", +"dtftx=abs(x1)\n", +"x=[3 2 1 2 3 2 1 2 3];\n", +"n=-4:4;\n", +"a=gca();\n", +"a.y_location='origin';\n", +"a.x_location='origin';\n", +"plot2d3('gnn',n,x);\n", +"xtitle('discrete periodic time signal');\n", +"x2=[dtftx dtftx 8];\n", +"a=gca();\n", +"xset('window',1);\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d3('gnn',n,x2);\n", +"xtitle('DTFT of discrete periodic signal');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.5: The_DFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"x=[1 0 2 0 3];//one period of signal\n", +"n=0:4;\n", +"k=0:4;\n", +"x1=x*exp(%i*n'*2*k*%pi/4)\n", +"DTFTx=abs(x1)\n", +"DFT=dft(x,-1)\n", +"DFS=DFT/5" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.7: Frequency_Response_of_Recursive_Filter.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"a=0.5;b=1;\n", +"n=0:50;\n", +"h=b*(a^n);\n", +"//Discrete-Time Fourier transform\n", +"K=500;\n", +"k=-250:1:250;\n", +"w=%pi*k/K;\n", +"H=h*exp(-%i*n'*w);\n", +"//caluculation of phase and magnitude of h(z)\n", +"[phase_H,m]=phasemag(H);\n", +"H=abs(H);\n", +"a=gca();\n", +"subplot(2,1,1);\n", +"a.y_location='origin';\n", +"plot2d(w/%pi,H);\n", +"xlabel('Frequency in radians')\n", +"ylabel('abs(H)')\n", +"title('magnitude Response')\n", +"subplot(2,1,2);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(w/(2*%pi),phase_H)\n", +"xlabel('Frequency in Radians');\n", +"ylabel('<(H)')\n", +"title('Phase Response'))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.8a: The_DTFT_in_System_Analysis.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT in system analysis\n", +"a=0.5;b=1;\n", +"n=0:50;\n", +"h=b*(a^n);\n", +"//Discrete-Time Fourier transform\n", +"K=500;\n", +"k=0:1:K;\n", +"w=%pi*k/K;\n", +"H=h*exp(-%i*n'*w);\n", +"//x[n] is given as (a)^n*u[n]\n", +"xw=h*exp(-%i*n'*w);\n", +"for i=1:501\n", +" y(i)=H(i)*xw(i);\n", +"end\n", +"[phase_y,m]=phasemag(y);\n", +"y=real(y);\n", +"subplot(2,1,1)\n", +"plot2d(w/%pi,y);\n", +"xlabel('Frequency in radians')\n", +"ylabel('abs(y)')\n", +"title('magnitude Response')\n", +"subplot(2,1,2)\n", +"plot2d(w/%pi,phase_y)\n", +"xlabel('Frequency in Radians');\n", +"ylabel('<(y)')\n", +"title('Phase Response')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.8b: The_DTFT_in_System_Analysis.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"a=0.5;b=1;\n", +"n=0:50;\n", +"h=4*(a^n);\n", +"//Discrete-Time Fourier transform\n", +"K=500;\n", +"k=0:1:K;\n", +"w=%pi*k/K;\n", +"H=h*exp(-%i*n'*w);\n", +"//x[n] is given as (a)^n*u[n]\n", +"x=4*[ones(1:51)];\n", +"xw=x*exp(%i*n'*w);\n", +"for i=1:501\n", +" y(i)=H(i)*xw(i);\n", +"end\n", +"[phase_y,m]=phasemag(y);\n", +"y=real(y);\n", +"subplot(2,1,1);\n", +"plot2d(w/%pi,y);\n", +"xlabel('Frequency in radians')\n", +"ylabel('abs(y)')\n", +"title('magnitude Response')\n", +"subplot(2,1,2)\n", +"plot2d(w/%pi,phase_y)\n", +"xlabel('Frequency in Radians');\n", +"ylabel('<(y)')\n", +"title('Phase Response')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.9a: DTFT_and_steady_state_response.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT and steady state response \n", +"a=0.5,b=1;F=0.25;\n", +"n=0:(5/1000):5;\n", +"h=(a^n);\n", +"x=10*cos(0.5*%pi*n'+%pi/3);\n", +"H=h*exp(-%i*n'*F);\n", +"Yss=H*x;\n", +"[phase_Yss,m]=phasemag(Yss);\n", +"Yss=real(Yss);\n", +"subplot(2,1,1)\n", +"plot2d(n,Yss);\n", +"xlabel('Frequency in radians')\n", +"ylabel('abs(Yss)')\n", +"title('magnitude Response')\n", +"subplot(2,1,2)\n", +"plot2d(n,phase_Yss)\n", +"xlabel('Frequency in Radians');\n", +"ylabel('<(y)')\n", +"title('Phase Response')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.9b: DTFT_and_steady_state_response.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT and steady state response \n", +"a=0.8,b=1;F=0;\n", +"n=0:50;\n", +"h=(a^n);\n", +"x=4*[ones(1:10)];\n", +"H=h*exp(-%i*n'*F)\n", +"Yss=H*x" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/6-Filter_Concepts.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/6-Filter_Concepts.ipynb new file mode 100644 index 0000000..aa52a00 --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/6-Filter_Concepts.ipynb @@ -0,0 +1,306 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 6: Filter Concepts" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.1: The_Minimum_Phase_Concept.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//The minimum phase concept\n", +"z=%z;\n", +"F=0:(0.5/400):0.5;\n", +"z=exp(%i*2*%pi*F);\n", +"for i=1:401\n", +"H1Z(i)=((z(i)-1/2)*(z(i)-1/4))/((z(i)-1/3)*(z(i)-1/5));\n", +"end\n", +"for i=1:401\n", +"H2Z(i)=(((-1/2)*z(i)+1)*(z(i)-1/4))/((z(i)-1/3)*(z(i)-1/5));\n", +"end\n", +"for i=1:401\n", +"H3Z(i)=(((-1/2)*z(i)+1)*((-1/4)*z(i)+1))/((z(i)-1/3)*(z(i)-1/5));\n", +"end\n", +"[phase_H1Z,m]=phasemag(H1Z);\n", +"[phase_H2Z,m]=phasemag(H2Z);\n", +"[phase_H3Z,m]=phasemag(H3Z);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"xlabel('Digital Frequency F');\n", +"ylabel('phase[degrees]');\n", +"xtitle('phase of three filters');\n", +"plot2d(F,phase_H1Z,rect=[0,-200,0.5,200]);\n", +"plot2d(F,phase_H2Z,rect=[0,-200,0.5,200]);\n", +"plot2d(F,phase_H3Z,rect=[0,-200,0.5,200]);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.4: Linear_Phase_Filters.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//linear phase filters\n", +"z=%z\n", +"H1Z=((z^3)+2*(z^2)+2*z+1)/(z^3);\n", +"//from pole zero diagram its not a linear phase filter\n", +"H2Z=(z^4+4.25*z^2+1)/(z^4);\n", +"xset('window',1);\n", +"plzr(H2Z);\n", +"//from pole zero diagram and LPF\n", +"// characteristics its a linear phase filter\n", +"H3Z=((z^4+2.5*z^3-2.5*z-1)/(z^4));\n", +"xset('window',2);\n", +"plzr(H3Z);\n", +"//from pole zero diagram and LPF\n", +"// characteristics its a linear phase filter" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.6: Frequency_Response_and_Filter_characteristics.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Frequency Response and filter characteristics\n", +"z=%z;\n", +"F=0:(0.5/200):0.5;\n", +"z=exp(%i*2*%pi*F);\n", +"H1=(1/3)*(z+1+z^-1);\n", +"H2=(z/4)+(1/2)+(1/4)*(z^-1);\n", +"H1=abs(H1);\n", +"H2=abs(H2);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"subplot(211);\n", +"plot2d(F,H1);\n", +"xlabel('Digital frequency F');\n", +"ylabel('impuse function H1(f)');\n", +"subplot(212);\n", +"plot2d(F,H2);\n", +"xlabel('Digital frequency F');\n", +"ylabel('impuse function H1(f)');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.7a: Filters_and_Pole_Zero_Plots.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"z=%z;\n", +"s=%s;\n", +"F=0:(0.5/400):0.5;\n", +"s=exp(%i*2*%pi*F);\n", +"H1Z=(z^4+1)/(z^4+1.6982*z^2+0.7210);\n", +"for i=1:401\n", +" H1(i)=(s(i)^4+1)/(s(i)^4+1.6982*s(i)^2+0.7210);\n", +"end\n", +"H1=abs(H1);\n", +"plzr(H1Z);\n", +"a=gca();\n", +"xset('window',1);\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(F,H1)\n", +"xlabel('Digital frequency F');\n", +"ylabel('magnitude');\n", +"xtitle('Magnitude spectrum of bandpass filter');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.7b: Filters_and_Pole_Zero_Plots.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"z=%z;\n", +"s=%s;\n", +"F=0:(0.5/400):0.5;\n", +"s=exp(%i*2*%pi*F);\n", +"H1Z=(z^2+1-0.618*z)/(z^2-0.5857*z+0.898);\n", +"for i=1:401\n", +" H1(i)=(s(i)^2+1-0.618*s(i))/(s(i)^2-0.5857*s(i)+0.898);\n", +"end\n", +"H1=abs(H1);\n", +"plzr(H1Z);\n", +"a=gca();\n", +"xset('window',1);\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(F,H1)\n", +"xlabel('Digital frequency F');\n", +"ylabel('magnitude');\n", +"xtitle('Magnitude spectrum of bandpass filter');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.8: Digital_resonator_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Digital Resonator design with peak gain 50 HZ \n", +"//and 3 db bandwidth of 6HZ at sampling of 300 HZ\n", +"clf();\n", +"s=%s;\n", +"F=0:150;\n", +"f=F/300;\n", +"s=exp(%i*2*%pi*f);\n", +"for i=1:151\n", +" H1(i)=(0.1054*(s(i)^2))/(s(i)^2-0.9372*s(i)+0.8783);\n", +"end\n", +"H1=abs(H1);\n", +"H2=H1(40:60);\n", +"F1=40:60;\n", +"f1=F1/300;\n", +"a=gca();\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(F,H1)\n", +"xlabel('Analog frequency F');\n", +"ylabel('magnitude');\n", +"xtitle('Magnitude spectrum of digital resonator with peak 50HZ');\n", +"xset('window',1);\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(F1,H2)\n", +"xlabel('Analog frequency F');\n", +"ylabel('magnitude');\n", +"xtitle('passband detail');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.9: Periodic_Notch_Filter_Desig.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Periodic notch filter design at 60 HZ and sampling frequency 300HZ\n", +"z=%z;\n", +"f=0:(0.5/400):0.5;\n", +"z1=exp(%i*2*%pi*f);\n", +"for i=1:401\n", +" H1Z(i)=(z1(i)^5-1)/((z1(i)^5)-(0.9^5));\n", +" H2Z(i)=(z1(i)^5-1)/((z1(i)^5)-(0.99^5));\n", +"end\n", +"H1Z=abs(H1Z);\n", +"H2Z=abs(H2Z);\n", +"N1z=(1-z^-5)/(1-z^-1);\n", +"H3z=(N1z)/(horner(N1z,z/0.9));\n", +"H4z=(N1z)/(horner(N1z,z/0.99));\n", +"H3z=horner(H3z,z1);\n", +"H4z=horner(H4z,z1);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"a.y_location='origin';\n", +"plot2d(f,H1Z);\n", +"plot2d(f,H2Z);\n", +"xlabel('Digital frequency f');\n", +"ylabel('magnitude');\n", +"xtitle('Periodic Notch Filter N=5,R=0.9,0.99');\n", +"xset('window',1);\n", +"plot2d(f,H3z);\n", +"plot2d(f,H4z);\n", +"xlabel('Digital frequency f');\n", +"ylabel('magnitude');\n", +"xtitle('Notch Filter that also passes DC N=5,R=0.9,0.99');" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/7-Digital_Processing_of_Analog_Signals.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/7-Digital_Processing_of_Analog_Signals.ipynb new file mode 100644 index 0000000..ab88b1e --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/7-Digital_Processing_of_Analog_Signals.ipynb @@ -0,0 +1,406 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 7: Digital Processing of Analog Signals" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.10: ADC_considerations.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//ADC considerations\n", +"//(a)Aperture time TA\n", +"B=12;\n", +"fo=15000;//band limited frquency\n", +"TAm=(1/((2^B)))/(%pi*fo);\n", +"TAm=TAm*10^9\n", +"//Hence TA must satisfy TA<=TAm nano sec\n", +"//(b)conversion time of quantizer\n", +"TA=4*10^-9;\n", +"TH=10*10^-6;//hold time\n", +"S=30*10^3;\n", +"TCm=1/S-TA-TH;\n", +"TCm=TCm*10^6\n", +"//Hence TC must satisfy TC<=TCm micro sec\n", +"//(c)Holding capacitance C\n", +"Vo=10;\n", +"TH=10*10^-6;\n", +"B=12;\n", +"R=10^6;//input resistance\n", +"delv=Vo/(2^(B+1));\n", +"Cm=(Vo*TH)/(R*delv);\n", +"Cm=Cm*10^9\n", +"//Hence C must satisfy C>=Cm nano farad" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.11: Anti_Aliasing_Filter_Considerations.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Anti Aliasing filter considerations\n", +"//minimum stop band attenuation As\n", +"B=input('enter no. of bits');//no. of samples\n", +"n=input('enter band width in KHZ');\n", +"As=20*log10(2^B*sqrt(6))\n", +"//nomalised frequency\n", +"Vs=(10^(0.1*As)-1)^(1/(2*n))\n", +"fp=4;//pass edge frequency\n", +"fs=Vs*fp//stop band frquency\n", +"S=2*fs//sampling frequency\n", +"fa=S-fp//aliaed frequency\n", +"Va=fa/fp;\n", +"//Attenuation at aliased frequency\n", +"Aa=10*log10(1+Va^(2*n))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.12: Anti_Imaging_Filter_Considerations.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Anti Imaging Filter considerations\n", +"Ap=0.5;//passband attenuation\n", +"fp=20;//passband edge frequency\n", +"As=60;//stopband attenuation\n", +"S=42.1;\n", +"fs=S-fp;//stopband edge frequency\n", +"e=sqrt(10^(0.1*Ap)-1);\n", +"e1=sqrt(10^(0.1*As)-1);\n", +"n=(log10(e1/e))/(log10(fs/fp));\n", +"n=ceil(n)//design of nth order butworth filter\n", +"//(b)Assuming Zero-order hold sampling\n", +"S1=176.4;\n", +"fs1=S1-fp;\n", +"Ap=0.316;\n", +"e2=sqrt(10^(0.1*Ap)-1);\n", +"n1=(log10(e1/e2))/(log(fs1/fp));//new order of butworth filter\n", +"n1=ceil(n1)\n", +"f=0:100;\n", +"x=abs(sinc(f*%pi/S));\n", +"f1=0:500;\n", +"x1=abs(sinc(f1*%pi/S1));\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d(f,x);\n", +"xtitle('spectra under normal sampling condition','f(kHZ)','sinc(f/s1)');\n", +"subplot(212);\n", +"plot2d(f1,x1);\n", +"xtitle('spectra under over sampling condition','f(kHZ)','sinc(f/s1)');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.3: Sampling_oscilloscope.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Sampling Oscilloscope Concepts\n", +"fo=100;a=50;\n", +"s=(a-1)*fo/a;\n", +"B=100-s;\n", +"i=s/(2*B);\n", +"i=ceil(i);\n", +"disp(i,'The sampling frequency can at max divided by i');\n", +"disp(s,2*B,'range of sampling rate is between s and 2*B');\n", +"fo1=100;\n", +"a=50;\n", +"s1=(a-1)*fo1/a;\n", +"B1=400-4*s1;\n", +"j=s1/(2*B1);\n", +"j=ceil(j);\n", +"disp(j,'The sampling frequency can at max divided by j');\n", +"disp(s1,2*B1,'range of sampling rate is between s1 and 2*B1');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.4: Sampling_of_Band_pass_signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//sampling of bandpass signals\n", +"fc=4;fl=6;\n", +"B=fl-fc;\n", +"xt=[0 1 2 1];\n", +"xtt=[0 1 2];\n", +"a=0;b=1;c=2;\n", +"xta=[xt];\n", +"xtb=[0 0 2 1 0];\n", +"xtc=[0 0 0 2 1 0];\n", +"xt1=[xta xta xta]; \n", +"xt2=[xtb xtb(length(xtb):-1:2) xtb(2:length(xtb)) xtb(length(xtb):-1:2)];\n", +"xt3=[xtc(length(xtc):-1:2) xtc(3:length(xtc)) zeros(1:7) xtc(length(xtc):-1:2) xtc(3:length(xtc))];\n", +"f1=0:length(xt1)-1;\n", +"f2=[0 1 1.001 2:6 6.001 7 7.001 8:12 12.001];\n", +"f3=[-10:-8 -7.99 -7:-6 -5.99 -5:6 6.01 7:8 8.01 9:10];\n", +"subplot(211);\n", +"plot2d(f1,xt1);\n", +"xtitle('spectrum of signal sampled at 4KHZ');\n", +"subplot(212);\n", +"plot2d(f2,xt2);\n", +"xtitle('spectrum of signal sampled at 7KHZ');\n", +"xset('window',1);\n", +"b=gca();\n", +"b.y_location='origin'\n", +"plot2d(f3,xt3);\n", +"xtitle('spectrum of signal sampled at 14KHZ');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.6: Signal_Reconstruction_from_Samples.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//signal reconstruction from samples\n", +"//(a)By step interpolation method\n", +"x=[-1 2 3 2];\n", +"t=2.5;\n", +"ts=1;\n", +"t1=ceil(t);\n", +"t2=floor(t);\n", +"x1t=x(t2)\n", +"//(b)By linear interpolation method\n", +"x2t=(x(t1)+x(t2))/2\n", +"//(c)By sinc interpolation method\n", +"x3t=0;x1=[1 2 3 4];\n", +"for k=1:4\n", +" x3t=x3t+(x1(k)*sinc(%pi*(t-(k-1))));\n", +"end\n", +"x3t//sinc interpolated value of x(2.5)\n", +"//(d)raised cosine interpolation method\n", +"x4t=0;\n", +"for k=1:4\n", +" p=(cos(0.5*%pi*(t-k+1))/(1-(t-k+1)^2));\n", +" xt=x1(k)*sinc(%pi*(t-k+1))*p;\n", +" x4t=x4t+xt;\n", +"end\n", +"x4t//raised cosine interpolated value of x(2.5)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.7: Zero_Interpolation_and_Spectrum_Replication.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Zero interpolation and spectrum replication\n", +"XF=[0 1 2 1];\n", +"X1F=[XF XF XF 0];\n", +"YF=[X1F X1F];\n", +"DF=0.5*[XF XF 0];\n", +"GF=0.5*[XF 0 XF 0 XF 0];\n", +"f=-0.2:0.1:1;\n", +"f1=-0.1:0.05:1.15;\n", +"f2=-0.4:0.2:1.2;\n", +"f3=-0.2:0.1:1.2;\n", +"length(f3),length(GF)\n", +"a=gca();\n", +"a.y_location='origin';\n", +"subplot(211);\n", +"plot2d(f,X1F);\n", +"ylabel('X1F');\n", +"subplot(212);\n", +"a.y_location='origin';\n", +"plot2d(f1,YF);\n", +"ylabel('YF');\n", +"xset('window',1);\n", +"b=gca();\n", +"b.y_location='origin';\n", +"subplot(211);\n", +"plot2d(f2,DF);\n", +"ylabel('DF');\n", +"subplot(212);\n", +"b.y_location='origin';\n", +"plot2d(f3,GF);\n", +"ylabel('GF');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.8: Up_Sampling_and_Filtering.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clf();\n", +"X=[0 0.5 1 0.5];\n", +"XF=[X 0];\n", +"WF=[X X X 0];\n", +"f=-0.5:0.25:0.5;\n", +"f1=-0.75:0.125:0.75;\n", +"HF=[0 1 1 1 0];\n", +"f2=[-0.126,-0.125:0.125:0.125,0.126];\n", +"for i=1:5\n", +" YF(i)=WF(i)*HF(i);\n", +"end\n", +"f3=[-0.126 -0.125 0 0.125 0.126];\n", +"a=gca();\n", +"a.y_location='origin';\n", +"subplot(211);\n", +"plot2d(f,XF);\n", +"xtitle('spectrum of XF');\n", +"a.y_location='origin';\n", +"subplot(212);\n", +"plot2d(f1,WF);\n", +"xtitle('spectrum of WF');\n", +"xset('window',1);\n", +"b=gca();\n", +"b.y_location='origin';\n", +"subplot(211);\n", +"plot2d(f2,HF);\n", +"xtitle('spectrum of HF');\n", +"b.y_location='origin';\n", +"subplot(212);\n", +"plot2d(f3,YF);\n", +"xtitle('spectrum of YF');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.9: Quantisation_Effects.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//(a)Quantisation effects\n", +"sig=0.005;\n", +"D=4;\n", +"B=log2(D/(sig*sqrt(12)));//no.of samples\n", +"//value of B to ensure quantisation error to 5mv\n", +"//(b)Quantisation error and noise\n", +"xn=0:0.2:2.0;\n", +"xqn=[0 0 0.5 0.5 1 1 1 1.5 1.5 2 2];\n", +"en=xn-xqn;//quantization error\n", +"//Quantisation signal top noise ratio\n", +"x=0;e=0;\n", +"for i=1:length(xn)\n", +" x=x+xn(i)^2;\n", +" e=e+en(i)^2;\n", +"end\n", +"//method 1\n", +"SNRQ=10*log10(x/e)\n", +"//method 2\n", +"SNRQ=10*log10(x/length(xn))+10.8+20*log10(4)-20*log10(2)\n", +"SNRS=10*log10(1.33)+10*log10(12)+20*log10(4)-20*log10(2)\n", +"//from results we see that SNRS is statistical estimate" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/8-The_Discrete_Fourier_Transform_and_its_Applications.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/8-The_Discrete_Fourier_Transform_and_its_Applications.ipynb new file mode 100644 index 0000000..16b6cfe --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/8-The_Discrete_Fourier_Transform_and_its_Applications.ipynb @@ -0,0 +1,925 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 8: The Discrete Fourier Transform and its Applications" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.10: DFS_of_sampled_Periodic_Signals.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DFS of sampled periodic signals\n", +"xn=[0 ones(2:16) 0 -ones(18:32)];\n", +"XDFS=0.032*dft(xn,-1);\n", +"for i=1:length(XDFS)\n", +" if (abs(XDFS(i))<0.000001) then\n", +" XDFS(i)=0;\n", +" end\n", +"end\n", +"disp(XDFS,'The DFS of x[n] is');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.11: The_effects_of_leakage.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Effects of leakage\n", +"n1=0:0.005:0.1;\n", +"n2=0:0.005:0.125;\n", +"n3=0:0.005:1.125;\n", +"xt1=(2*cos(20*%pi*n1')+5*cos(100*%pi*n1'));\n", +"xt2=(2*cos(20*%pi*n2')+5*cos(100*%pi*n2'));\n", +"xt3=(2*cos(20*%pi*n3')+5*cos(100*%pi*n3'));\n", +"XDFS1=abs(dft(xt1,-1))/20;\n", +"XDFS2=abs(dft(xt2,-1))/25;\n", +"XDFS3=abs(dft(xt3,-1))/225;\n", +"f1=0:5:100;\n", +"f2=0:4:100;\n", +"f3=0:100/225:100;\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d3('gnn',f1,XDFS1);\n", +"xlabel('analog frequency');\n", +"ylabel('Magnitude');\n", +"xset('window',1);\n", +"subplot(211);\n", +"plot2d3('gnn',f2,XDFS2);\n", +"xlabel('analog frequency');\n", +"ylabel('Magnitude');\n", +"subplot(212);\n", +"plot2d3('gnn',f3,XDFS3);\n", +"xlabel('analog frequency');\n", +"ylabel('Magnitude');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.15a: Methods_to_find_convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//overlapp-add and overlap-save methods of convolution\n", +"//overlap-add method\n", +"xn=[1 2 3 3 4 5];\n", +"xon=[1 2 3];\n", +"x1n=[3 4 5];\n", +"hn=[1 1 1];\n", +"yon=convol(xon,hn);\n", +"y1n=convol(x1n,hn);\n", +"yon=[yon,0,0,0];\n", +"y1n=[0,0,0,y1n];\n", +"yn=yon+y1n\n", +"yn1=convol(xn,hn) " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.15b: Methods_to_find_convolution.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//(b)overlap-save method\n", +"xn=[1 2 3 3 4 5];\n", +"hn=[1 1 1];\n", +"xon=[0 0 1 2 3];\n", +"x1n=[2 3 3 4 5];\n", +"x2n=[4 5 0 0 0];\n", +"yon=convol(xon,hn);\n", +"y1n=convol(x1n,hn);\n", +"y2n=convol(x2n,hn);\n", +"yno=yon(3:5);\n", +"yn1=y1n(3:5);\n", +"yn2=y2n(3:5);\n", +"yn=[yno yn1 yn2]\n", +"YN=convol(xn,hn)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.16: Signal_Interpolation_using_FFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//signal interpolation using FFT\n", +"xn=[0 1 0 -1];\n", +"XDFT=dft(xn,-1)\n", +"ZT=[0 -2*%i 0 zeros(1:27) 0 2*%i];\n", +"xn1=dft(ZT,1);\n", +"t=0:1/length(xn1):1-(1/length(xn1));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(t,xn1);\n", +"xlabel('time t');\n", +"ylabel('Amplitude');\n", +"xtitle('Interpolated Sinusoid:4 samples over one period');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.17: The_Concept_of_Periodogram.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//concept of periodogram\n", +"xn=[0 1 0 -1];\n", +"N=4;\n", +"XDFT=dft(xn,-1);\n", +"for i=1:length(XDFT)\n", +" p(i)=(1/N)*abs(XDFT(i)^2);\n", +"end\n", +"p//periodogram" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.18: DFT_from_matrix_formulatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//The DFT from the matrix formulation\n", +"xn=[1;2;1;0];\n", +"w=exp(-%i*%pi/2);\n", +"for i=1:4\n", +" for j=1:4\n", +" WN(i,j)=w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"XDFT=WN*xn" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.19: Using_DFT_to_find_IDFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//using DFT to find IDFT\n", +"XDFT=[4;-2*%i;0;2*%i];\n", +"XDFTc=[4;2*%i;0;-2*%i];\n", +"w=exp(-%i*%pi/2);\n", +"for i=1:4\n", +" for j=1:4\n", +" WN(i,j)=w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"xn=1/4*(WN*XDFTc)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.1: DFT_from_Defining_Relation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DFT from defining relation\n", +"//N-point DFT\n", +"x=[1 2 1 0];\n", +"XDFT=dft(x,-1);\n", +"disp(XDFT,'The DFT of x[n] is');\n", +"//DFT of periodic signal x with period N=4" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.20: Decimation_in_Frequency_FFT_algorithm.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//A four point decimation-in-frequency FFT algorithm\n", +"x=[1 2 1 0];\n", +"w=-%i;\n", +"xdft(1)=x(1)+x(3)+x(2)+x(4);\n", +"xdft(2)=x(1)-x(3)+w*(x(2)-x(4));\n", +"xdft(3)=x(1)+x(3)-x(2)-x(4);\n", +"xdft(4)=x(1)-x(3)-w*(x(2)-x(4));\n", +"XDFT=dft(x,-1);\n", +"xdft,XDFT" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.21: Decimation_in_time_FFT_algorithm.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//A four point decimation-in-time FFT algorithm\n", +"x=[1 2 1 0];\n", +"w=-%i;\n", +"xdft=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:4\n", +" xdft(i)=xdft(i)+x(j)*w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"XDFT=dft(x,-1);\n", +"xdft,XDFT" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.22: 4_point_DFT_from_3_point_sequence.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//A 4-point DFT from a 3-point sequence\n", +"xn=[1;2;1];\n", +"w=exp(-%i*%pi/2);\n", +"for i=1:4\n", +" for j=1:3\n", +" WN(i,j)=w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"XDFT=WN*xn" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.23: 3_point_IDFT_from_4_point_DFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//A 3-point IDFT from 4-point DFT\n", +"XDFT=[4;-2*%i;0;2*%i];\n", +"w=exp(-%i*%pi/2);\n", +"for i=1:4\n", +" for j=1:3\n", +" WN(i,j)=w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"WI=WN';\n", +"xn=1/4*(WI*XDFT)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.24: The_importance_of_Periodic_Extension.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//The importance of Periodic extension\n", +"//(a)For M=3\n", +"x=[1 2 1];\n", +"XDFT=dft(x,-1)\n", +"w=exp(-%i*2*%pi/3);\n", +"for i=1:3\n", +" for j=1:3\n", +" WN(i,j)=w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"WI=WN';\n", +"xn=1/3*WI*XDFT\n", +"//The result is periodic with M=3 & 1 period equals x[n]\n", +"//(b)For M=4\n", +"y=[1 2 1 0];\n", +"YDFT=dft(y,-1)\n", +"w=exp(-%i*%pi/2);\n", +"for i=1:4\n", +" for j=1:4\n", +" WN(i,j)=w^((i-1)*(j-1));\n", +" end\n", +"end\n", +"WI=WN';\n", +"yn=1/4*WI*YDFT" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.2: The_DFT_and_conjugate_Symmetry.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//The DTFT and conjugate symmetry\n", +"//8-point DFT\n", +"x=[1 1 0 0 0 0 0 0];\n", +"XDFT=dft(x,-1);\n", +"disp(XDFT,'The DFT of x is');\n", +"disp('from conjugate symmetry we see XDFT[k]=XDFT[8-k]');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.3: Circular_Shift_and_Flipping.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Circular shift and flipping\n", +"//(a)right circular shift\n", +"y=[1 2 3 4 5 0 0 6];\n", +"f=y;g=y;h=y;\n", +"for i=1:2\n", +" b=f(length(f));\n", +" for j=length(f):-1:2\n", +" f(j)=f(j-1);\n", +" end\n", +" f(1)=b;\n", +"end\n", +"disp(f,'By right circular shift y[n-2] is');\n", +"//(b)left circular shift\n", +"for i=1:2\n", +" a=g(1);\n", +" for j=1:length(g)-1\n", +" g(j)=g(j+1);\n", +" end\n", +" g(length(g))=a;\n", +"end\n", +"disp(g,'By left circular shift y[n+2] is');\n", +"//(c)flipping property\n", +"h=[h(1) h(length(h):-1:2)];\n", +"disp(h,'By flipping property y[-n] is');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.4: Properties_of_DFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"x=[1 2 1 0];\n", +"XDFT=dft(x,-1)\n", +"//(a)time shift property\n", +"y=x;\n", +"for i=1:2\n", +" a=y(1);\n", +" for j=1:length(y)-1\n", +" y(j)=y(j+1);\n", +" end\n", +" y(length(y))=a;\n", +"end\n", +"YDFT=dft(y,-1)\n", +"disp(YDFT,'By Time-Shift property DFT of x[n-2] is'); \n", +"//(b)flipping property\n", +"g=[x(1) x(length(x):-1:2)]\n", +"GDFT=dft(g,-1)\n", +"disp(GDFT,'By Time reversal property DFT of x[-n] is'); \n", +"//(c)conjugation property\n", +"p=XDFT;\n", +"PDFT=[p(1);p(4:-1:2)];\n", +"disp(YDFT,'BY conjugation property DFT of x*[n] is'); " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.5a: Properties_of_DFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//properties of DFT\n", +"//a1)product\n", +"xn=[1 2 1 0];\n", +"XDFT=dft(xn,-1)\n", +"hn=xn.*xn\n", +"HDFT=dft(hn,-1)\n", +"HDFT1=1/4*(convol(XDFT,XDFT))\n", +"HDFT1=[HDFT1,zeros(8:12)];\n", +"HDFT2=[HDFT1(1:4);HDFT1(5:8);HDFT1(9:12)];\n", +"HDFT3=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:3\n", +" HDFT3(i)=HDFT3(i)+HDFT2(j,i);\n", +" end\n", +"end\n", +"disp(HDFT3,'DFT of x[n]^2 is');\n", +"//a2)periodic convolution\n", +"vn=convol(xn,xn);\n", +"vn=[vn,zeros(8:12)];\n", +"vn=[vn(1:4);vn(5:8);vn(9:12)];\n", +"vn1=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:3\n", +" vn1(i)=vn1(i)+vn(j,i);\n", +" end\n", +"end\n", +"VDFT=dft(vn1,-1);\n", +"VDFT1=XDFT.*XDFT;\n", +"disp(VDFT1,'DFT of x[n]*x[n] is');\n", +"//a3)signal energy(parcewell's theorem)\n", +"xn2=xn^2;\n", +"E=0;\n", +"for i=1:length(xn2)\n", +" E=E+abs(xn2(i));\n", +"end\n", +"XDFT2=XDFT^2\n", +"E1=0;\n", +"for i=1:length(XDFT2)\n", +" E1=E1+abs(XDFT2(i));\n", +"end\n", +"E,(1/4)*E1;\n", +"disp(1/4*E1,'The energy of the signal is');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.5b: Properties_of_DFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//b1)modulation\n", +"XDFT=[4 -2*%i 0 2*%i];\n", +"xn=dft(XDFT,1)\n", +"for i=1:length(xn)\n", +" zn(i)=xn(i)*%e^((%i*%pi*(i-1))/2);\n", +"end\n", +"disp(zn,'The IDFT of XDFT[k-1] is');\n", +"ZDFT=[2*%i 4 -2*%i 0];\n", +"zn1=dft(ZDFT,1)\n", +"//b2)periodic convolution\n", +"HDFT=(convol(XDFT,XDFT))\n", +"HDFT=[HDFT,zeros(8:12)];\n", +"HDFT=[HDFT(1:4);HDFT(5:8);HDFT(9:12)];\n", +"HDFT1=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:3\n", +" HDFT1(i)=HDFT1(i)+HDFT(j,i);\n", +" end\n", +"end\n", +"HDFT1;\n", +"hn=dft(HDFT1,1)\n", +"hn1=4*(xn.*xn);\n", +"disp(hn1,'The IDFT of XDFT*XDFT is');\n", +"//b3)product\n", +"WDFT=XDFT.*XDFT;\n", +"wn=dft(WDFT,1)\n", +"wn1=convol(xn,xn);\n", +"wn1=[wn1,zeros(8:12)];\n", +"wn1=[wn1(1:4);wn1(5:8);wn1(9:12)];\n", +"WN=[0 0 0 0];\n", +"for i=1:4\n", +" for j=1:3\n", +" WN(i)=WN(i)+wn1(j,i);\n", +" end\n", +"end\n", +"disp(WN,'The IDFT of XDFT.XDFT is');\n", +"//b4)Central ordinates and signal Energy\n", +"E=0;\n", +"for i=1:length(xn)\n", +" E=E+abs(xn(i)^2);\n", +"end\n", +"disp(E,'the signal energy is'); " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.5c: Properties_of_DFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Regular convolution\n", +"xn=[1 2 1 0];\n", +"yn=[1 2 1 0 0 0 0];\n", +"YDFT=dft(yn,-1)\n", +"SDFT=YDFT.*YDFT\n", +"sn=dft(SDFT,1)\n", +"sn1=convol(xn,xn)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.6: Signal_and_Spectrum_Replication.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Signal and spectrum replication\n", +"xn=[2 3 2 1];\n", +"XDFT=dft(xn,-1)\n", +"yn=[xn xn xn];\n", +"YDFT=dft(yn,-1)\n", +"YDFT1=3*[XDFT(1:1/3:length(XDFT))];\n", +"for i=2:3\n", +" YDFT1(i:3:length(YDFT1))=0;\n", +"end\n", +"YDFT1(12:-1:11)=0;\n", +"disp(YDFT1,'the DFT of x[n/3] is');\n", +"hn=[xn(1:1/3:length(xn))]\n", +"for i=2:3\n", +" hn(i:3:length(hn))=0;\n", +"end\n", +"hn(12:-1:11)=0;\n", +"hn\n", +"HDFT=dft(hn,-1)\n", +"HDFT1=[XDFT;XDFT;XDFT];\n", +"disp(HDFT1,'the DFT of y[n]=[x[n],x[n],x[n]] is');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.7: Relating_DFT_and_DTFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//relating DFT and IDFT\n", +"XDFT1=[4 -2*%i 0 2*%i];\n", +"xn1=dft(XDFT1,1);\n", +"disp(xn1,'The IDFT of XDFT1');\n", +"XDFT2=[12 -24*%i 0 4*%e^(%i*%pi/4) 0 4*%e^(-%i*%pi/4) 0 24*%i];\n", +"xn2=dft(XDFT2,1);\n", +"disp(xn2,'The IDFT of XDFT1'); " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.8: Relating_DFT_and_DTFT.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Relating DFT and DTFT\n", +"xn=[1 2 1 0];\n", +"XDFT=dft(xn,-1);\n", +"//for F=k/4,k=0,1,2,3\n", +"for k=1:4\n", +" XF(k)=1+2*%e^(-%i*%pi*(k-1)/2)+%e^(-%i*%pi*(k-1));\n", +"end\n", +"XF,XDFT\n", +"disp(XF,'The DFT of x[n] is');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.9a: The_DFT_and_DFS_of_sinusoids.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DFT and DFS of sinusoids\n", +"n2=0:0.5/1000:5.5/100;\n", +"xt=4*cos(100*%pi*n2');\n", +"n=0:(0.5)/100:(5.5)/100;//F=3/12 hence N=12\n", +"xn=4*cos(100*%pi*n');\n", +"XDFT=dft(xn,-1);\n", +"n1=0:11;\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(n2,xt);\n", +"plot2d3('gnn',n,xn);\n", +"xset('window',1);\n", +"b=gca();\n", +"b.x_location='origin';\n", +"plot2d3('gnn',n1,XDFT);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.9b: The_DFT_and_DFS_of_sinusoids.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DFT and DFS of sinusoids\n", +"n2=0:1/1280:31/128;\n", +"xt=4*sin(72*%pi*n2');\n", +"n=0:1/128:31/128;//F=9/32 hence N=32\n", +"xn=4*sin(72*%pi*n');\n", +"XDFT=abs(dft(xn,-1));\n", +"n1=0:31;\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(n2,xt);\n", +"plot2d3('gnn',n,xn);\n", +"xset('window',1);\n", +"b=gca();\n", +"b.x_location='origin';\n", +"plot2d3('gnn',n1,XDFT);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.9c: The_DFT_and_DFS_of_sinusoids.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DFT and DFS of sinusoids\n", +"n2=0:1/840:6/21;\n", +"xt=4*sin(72*%pi*n2')-6*cos(12*%pi*n2');\n", +"n=0:1/21:6/21;//F=3/12 hence N=12\n", +"xn=4*sin(72*%pi*n')-6*cos(12*%pi*n');\n", +"XDFT=abs(dft(xn,-1));\n", +"n1=0:6;\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(n2,xt);\n", +"plot2d3('gnn',n,xn);\n", +"xset('window',1);\n", +"b=gca();\n", +"b.x_location='origin';\n", +"plot2d3('gnn',n1,XDFT);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.9d: The_DFT_and_DFS_of_sinusoids.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DFT and DFS of sinusoids\n", +"n2=0:1/2400:23/240;\n", +"xt=1+4*sin(120*%pi*n2')+4*sin(40*%pi*n2');\n", +"n=0:1/240:23/240;//F=9/32 hence N=32\n", +"xn=1+4*sin(120*%pi*n')+4*sin(40*%pi*n');\n", +"XDFT=abs(dft(xn,-1));\n", +"n1=0:23;\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(n2,xt);\n", +"plot2d3('gnn',n,xn);\n", +"xset('window',1);\n", +"b=gca();\n", +"b.x_location='origin';\n", +"plot2d3('gnn',n1,XDFT);" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/9-Design_of_IIR_Filters.ipynb b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/9-Design_of_IIR_Filters.ipynb new file mode 100644 index 0000000..cd4b61e --- /dev/null +++ b/Digital_Signal_Processing_A_Modern_Introduction_by_A_Ashok/9-Design_of_IIR_Filters.ipynb @@ -0,0 +1,677 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 9: Design of IIR Filters" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.10a: Bilinear_Design_of_Second_Order_Filters.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Bilinear design of second order filters\n", +"s=%s;z=%z;\n", +"fo=6;Wo=2*%pi*fo/25;\n", +"delf=5;S=25;\n", +"B=cos(2*%pi*fo/25)\n", +"C=tan(%pi*delf/25)\n", +"HS=1/(s+1);\n", +"HZ=horner(HS,(z^2-(2*B*z)+1)/(C*(z^2)-C))\n", +"f=0:0.5:12.5;\n", +"HZ1=horner(HZ,exp(%i*2*%pi*f'/25));\n", +"HZ1=abs(HZ1);\n", +"W2=(%pi*delf/25)+acos(cos(Wo)*cos(%pi*delf/25))\n", +"W1=W2-(2*%pi*delf/25)\n", +"f1=S*W1/(2*%pi),f2=S*W2/(2*%pi)\n", +"f3=[f1;fo;f2];\n", +"HZf=abs(horner(HZ,exp(%i*2*%pi*f3'/25)));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(f,HZ1,rect=[0 0 13 1]);\n", +"plot2d3('gnn',f3,HZf);\n", +"xlabel('Analog Frequency f[kHZ]');\n", +"ylabel('Magnitude');\n", +"xtitle('Band pass filter fo=6kHZ,delf=5kHZ');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.10b: Bilinear_Design_of_Second_Order_Filters.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Bilinear design of second order filters\n", +"s=%s;z=%z;\n", +"f1=4;f2=9;\n", +"delf=f2-f1;S=25;\n", +"B=cos(%pi*(f1+f2)/25)/cos(%pi*(f2-f1)/25)\n", +"C=tan(%pi*delf/25)\n", +"HS=1/(s+1);\n", +"HZ=horner(HS,(z^2-(2*B*z)+1)/(C*(z^2)-C))\n", +"f=0:0.5:12.5;\n", +"HZ1=horner(HZ,exp(%i*2*%pi*f'/25));\n", +"HZ1=abs(HZ1);\n", +"fo=S*acos(B)/(2*%pi)\n", +"f3=[f1 fo f2]; \n", +"HZf=abs(horner(HZ,exp(-%i*2*%pi*f3'/25)));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(f,HZ1);\n", +"plot2d3('gnn',f3,HZf);\n", +"xlabel('Analog Frequency f[kHZ]');\n", +"ylabel('Magnitude');\n", +"xtitle('Band pass filter f1=4kHZ,f2=9kHZ');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.10c: Bilinear_Design_of_Second_Order_Filters.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Bilinear design of second order filters\n", +"s=%s;z=%z;\n", +"fo=40;Wo=2*%pi*fo/200;\n", +"delf=2;S=25;\n", +"delW=2*%pi*delf/200;\n", +"B=cos(2*%pi*fo/200)\n", +"K=0.557;\n", +"C=K*tan(0.5*delW)\n", +"HS=1/(s+1);\n", +"HZ=horner(HS,(z^2-(2*B*z)+1)/(C*(z^2)-C))\n", +"f=0:2:100;\n", +"f1=35:0.5:45;\n", +"HZ1=horner(HZ,exp(%i*2*%pi*f'/200));\n", +"HZ2=horner(HZ,exp(%i*2*%pi*f1'/200));\n", +"HZ1=abs(HZ1);\n", +"HZ2=abs(HZ2);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"subplot(211);\n", +"plot2d(f,HZ1);\n", +"xlabel('Analog Frequency f[kHZ]');\n", +"ylabel('Magnitude');\n", +"xtitle('peaking filter fo=40HZ,delf=2HZ');\n", +"subplot(212);\n", +"plot2d(f1,HZ2);\n", +"xtitle('Blowup of response 35HZ to 45HZ');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.11: Interference_Rejection.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//interference Rejection\n", +"//design oh high-Q and low-Q notch filters\n", +"s=%s;z=%z;\n", +"Q=50;\n", +"fo=60;S=300;\n", +"delf=fo/Q;\n", +"Wo=2*%pi*fo/S;\n", +"delW=2*%pi*delf/S;\n", +"C=tan(0.5*delW),B=cos(Wo)\n", +"HS=(s)/(s+1);\n", +"H1Z=horner(HS,(z^2-(2*B*z)+1)/(C*(z^2)-C))\n", +"Q1=5;delf1=fo/Q1;\n", +"delW1=2*%pi*delf1/S;\n", +"C1=tan(0.5*delW1),B1=cos(Wo)\n", +"H2Z=horner(HS,(z^2-(2*B1*z)+1)/(C1*(z^2)-C1))\n", +"f=0:0.5:150;\n", +"H1Z1=horner(H1Z,exp(%i*2*%pi*f'/S));\n", +"H2Z1=horner(H2Z,exp(%i*2*%pi*f'/S));\n", +"a=gca();\n", +"subplot(211);\n", +"plot2d(f,H1Z1);\n", +"xlabel('Analog Frequency f[Hz]');\n", +"ylabel('Magnitude');\n", +"xtitle('60 HZ notch filter with Q=50');\n", +"subplot(212);\n", +"plot2d(f,H2Z1);\n", +"xlabel('Analog Frequency f[Hz]');\n", +"ylabel('Magnitude');\n", +"xtitle('60 HZ notch filter with Q=5');\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.12: IIR_Filter_Desig.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//IIR filter design\n", +"//Design of chebyshev IIR filter with following specifications\n", +"fp1=1.6;fp2=1.8;fs1=3.2;fs2=4.8;//pass band edges\n", +"Ap=2;As=20;S=12;\n", +"s=%s;z=%z;\n", +"//(a)Indirect Bilinear design\n", +"W=2*%pi*[fp1 fp2 fs1 fs2]/S\n", +"C=2;\n", +"omega=2*tan(0.5*W');//prewarping each band edge frequency\n", +"epsilon=sqrt(10^(0.1*Ap)-1);\n", +"n=acosh(((10^(0.1*As)-1)/epsilon^2)^1/2)/(acosh(fs1/fp1));\n", +"n=ceil(n)\n", +"alpha=(1/n)*asinh(1/epsilon);\n", +"for i=1:n\n", +" B(i)=(2*i-1)*%pi/(2*n);\n", +"end\n", +"for i=1:n\n", +" p(i)=-sinh(alpha)*sin(B(i))+%i*cosh(alpha)*cos(B(i));\n", +"end\n", +"Qs=1;\n", +"for i=1:n\n", +" Qs=Qs*(s-p(i))\n", +"end\n", +"Qo=0.1634;\n", +"HPS=Qo/Qs\n", +"HBPS=horner(HPS,(s^2+1.5045^2)/(s*1.202))\n", +"HZ=horner(HBPS,2*(z-1)/(z+1))\n", +"f=0:0.001:0.5;\n", +"HZF=abs(horner(HZ,exp(%i*2*%pi*f')));\n", +"HBPF=abs(horner(HBPS,%i*2*%pi*f'));\n", +"a=gca();\n", +"plot2d(f,HZF);\n", +"plot2d(f,HBPF);\n", +"xlabel('Analog Frequency');\n", +"ylabel('magnitude');\n", +"xtitle('band pass filter designed by the bilinear transformation');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.1: Response_Invariant_Mappings.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Response in variant mappings\n", +"s=%s;z=%z;\n", +"HS=1/(s+1);\n", +"f=0:0.05:0.5;\n", +"HS1=horner(HS,(%i*%pi*2*f'));\n", +"ts=1;\n", +"HZ=z/(z-0.3679);\n", +"HZ1=horner(HZ,exp(%i*%pi*2*f'));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"subplot(211)\n", +"plot2d(f,HS1);\n", +"plot2d(f,HZ1);\n", +"xlabel('Analog frequency f(Hz)');\n", +"ylabel('Magnitude');\n", +"xtitle('magnitude of H(s) and H(z)');\n", +"HZ1=HZ1-0.582;//magnitude after gain matching at dc \n", +"b=gca();\n", +"b.x_location='origin';\n", +"subplot(212);\n", +"plot2d(f,HS1);\n", +"plot2d(f,HZ1);\n", +"xlabel('Analog frequency f(Hz)');\n", +"ylabel('Magnitude');\n", +"xtitle('magnitude after gain matching at DC');\n", +"//Impulse response of analog and digital filter\n", +"t=0:0.01:6;\n", +"ht=exp(-t');\n", +"n=0:6;\n", +"hn=exp(-n');\n", +"xset('window',1)\n", +"c=gca();\n", +"subplot(211);\n", +"plot2d(t,ht);\n", +"plot2d3('gnn',n,hn);\n", +"xlabel('DT index n and time t=nts');\n", +"ylabel('Amplitude');\n", +"xtitle('Impulse response of analog and digital filter');\n", +"//Step response of analog and digital filter\n", +"t=0:0.01:6;\n", +"st=1-exp(-t');\n", +"n=0:6;\n", +"sn=(%e-%e^(-n'))/(%e-1);\n", +"c=gca();\n", +"subplot(212);\n", +"plot2d(t,st);\n", +"plot2d3('gnn',n,sn);\n", +"xlabel('DT index n and time t=nts');\n", +"ylabel('Amplitude');\n", +"xtitle('Step response of analog and digital filter');\n", +"\n", +"\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.2: Impulse_Invariant_Mappings.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Impulse invariant mappings\n", +"//(a)converting H(s)=4s+7/s^2+5s+4 to H(z) using impulse invariance\n", +"s=%s;\n", +"z=%z;\n", +"HS=(4*s+7)/(s^2+5*s+4);\n", +"pfss(HS)\n", +"ts=0.5;\n", +"HZ=3*z/(z-%e^(-4*ts))+z/(z-%e^(-ts))\n", +"//(b)converting H(s)=4s+7/s^2+5s+4 to H(z) using impulse invariance\n", +"HS1=4/((s+1)*(s^2+4*s+5))\n", +"pfss(HS1);\n", +"HZ1=2*z/(z-%e^-ts)+(2*1.414*z^2*cos(-0.75*%pi)-2*1.414*(z/%e)*cos(0.5-0.75*%pi))/(z^2-2*(z/%e)*cos(0.5)+%e^-2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.3ab: Modified_Impulse_Invariant_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//(a)Impulse invariant design\n", +"s=%s;z=%z;\n", +"HS=1/(s+1);\n", +"H1s=horner(HS,3*s/%pi)\n", +"H1z=%pi/3*z/(z-%e^(-%pi/3))\n", +"//Modified inmpulse invariant design\n", +"HZ=z/(z-1/%e);\n", +"HMZ=0.5*(z+1/%e)/(z-1/%e);//modified transfer function\n", +"H1Z=HZ/horner(HZ,1)\n", +"HM1Z=HMZ/horner(HMZ,1)\n", +"f=0:0.05:0.5;\n", +"HZ1=horner(HZ,exp(%i*2*%pi*f'));\n", +"HMZ1=horner(HMZ,exp(%i*2*%pi*f'));\n", +"H1Z1=horner(H1Z,exp(%i*2*%pi*f'));\n", +"HM1Z1=horner(HM1Z,exp(%i*2*%pi*f'));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(f,HZ1);\n", +"plot2d(f,HMZ1);\n", +"plot2d(f,H1Z1);\n", +"plot2d(f,HM1Z1);\n", +"xlabel('digital frequency');\n", +"ylabel('Magnitude');\n", +"xtitle('Impulse invariant design of H(s)=1/s+1 (dashed)');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.3cd: Modified_Impulse_Invariant_Design.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//modified Impulse invariant Design\n", +"//(c)H(s)=4s+7/s^2+5s+4\n", +"s=%s;z=%z;\n", +"HS=(4*s+7)/(s^2+5*s+4);\n", +"[d1]=degree(numer(HS));\n", +"[d2]=degree(denom(HS));\n", +"HZ=((3*z)/(z-%e^-2))+(z/(z-%e^-0.5))\n", +"if (d2-d1==1) then\n", +" h=(4+7/%inf)/(1+5/%inf+4/%inf)\n", +" HMZ=HZ-0.5*h\n", +"else\n", +" HMZ=HZ\n", +"end\n", +"HS1=4/((s+1)*(s^2+4*s+5))\n", +"HZ1=(0.2146*z^2+0.093*z)/(z^3-1.2522*z^2+0.527*z-0.0821);\n", +"[d1]=degree(numer(HS1));\n", +"[d2]=degree(denom(HS1));\n", +"if (d2-d1==1) then\n", +" HMZ1=HZ1-0.5*h\n", +"else\n", +" HMZ1=HZ1\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.5: Mappings_from_Difference_Algorithms.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Mappings from difference algorithms\n", +"//Backward difference mappings\n", +"s=%s;z=%z;\n", +"ts=1;a=1;\n", +"HS=1/(s+a);\n", +"HZa=horner(HS,(z-1)/(z*ts))\n", +"z1=roots(denom(HZa))//for ts >0 HZa always stable\n", +"HZb=horner(HS,(z-1)/ts)\n", +"z2=roots(denom(HZb))//stable only for 0<ats<2\n", +"HZc=horner(HS,(z^2-1)/(2*z*ts))\n", +"z3=roots(denom(HZc))//magnitude of 1 pole is always>1 hence unstable\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.6: Mappings_From_Integration_Algorithms.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Mappings from integration algorithm\n", +"//(a)Trapezoidal integration algorithm\n", +"s=%s;z=%z;\n", +"a=input('enter the value of a')\n", +"ts=input('enter the value of sampling time')\n", +"HS=1/(s+1);\n", +"HZa=horner(HS,2*(z-1)/(ts*(z+1)))\n", +"za=roots(denom(HZa))//pole always less than 1 ,hence HZ is stable\n", +"//(b)simphson's algorithm\n", +"HZb=horner(HS,3*(z^2-1)/(ts*(z^2+4*z+1)))\n", +"zb=roots(denom(HZb))\n", +"//magnitude of 1 pole always greater than 1 ,hence HZ is unstable" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.7: DTFT_of_Numerical_Algorithms.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//DTFT of numerical algorithms\n", +"//(a)For trapezoidal numerical integrator\n", +"ieee(2)\n", +"F=0:0.01:0.2;\n", +"HTF=1/(%i*2*tan(%pi*F'));\n", +"HIF=1/(%i*2*%pi*F');\n", +"Ha=1-((%pi*F')^2)/3-((%pi*F')^4/45);\n", +"//(b)For simphson's numerical integrator\n", +"Hb=((2*%pi*F')).*((2+cos(2*%pi*F'))./(3*sin(2*%pi*F')));\n", +"//For forward difference operator\n", +"HFF=(%e^(%i*2*%pi*F'))-1;\n", +"HDF=1/(%i*2*%pi*F');\n", +"Hc=1+(%i*2*%pi*F')/2-(2*%pi*F')^2/6;\n", +"Hc=abs(Hc);\n", +"//for central difference operator\n", +"HCF=sin(2*%pi*F')./(%i*4*%pi*%pi*F'^2);\n", +"Hd=abs(sin(2*%pi*F')./(2*%pi*F'));\n", +"length(F),length(Ha)\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(F,Ha,rect=[0,0.8,0.2,1.1]);\n", +"plot2d(F,Hb);\n", +"xtitle('Magnitude spectrum of Integration algorithms','Digital Frequency F','Magnitude');\n", +"xset('window',1);\n", +"plot2d(F,Hc,rect=[0,0.8,0.2,1.1]);\n", +"plot2d(F,Hd);\n", +"xtitle('Magnitude spectrum of difference algorithms','Digital Frequency F','Magnitude');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.8a: Bilinear_Transformation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Bilinear transformation\n", +"//To convert bessel analog filter to digital filter\n", +"s=%s;\n", +"z=%z;\n", +"HS=3/(s^2+3*s+3);\n", +"Wa=4;//analog omega\n", +"Wd=%pi/2;//digital omega\n", +"T=(2/Wa)*(tan(Wd/2));\n", +"HZ=horner(HS,(2/T)*(z-1)/(z+1))\n", +"f=0:0.1:6;\n", +"HS1=horner(HS,(%i*4*f'/3));\n", +"HS1=abs(HS1);\n", +"HZ1=horner(HZ,exp(-%i*%pi*f'/6));\n", +"HZ1=abs(HZ1);\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(f,HS1);\n", +"plot2d(f,HZ1);\n", +"xlabel('Analog Frequency f[kHZ)');\n", +"ylabel('Magnitude');\n", +"xtitle('Bessel filter H(s) and digital filter H(z)');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.8b: Bilinear_Transformation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Bilinear transformation\n", +"//To convert twin-T notch analog filter to digital filter\n", +"s=%s;\n", +"z=%z;\n", +"HS=(s^2+1)/(s^2+4*s+1);\n", +"Wo=1;\n", +"S=240;f=60;//sampling and analog frequencies\n", +"W=0.5*%pi;//digital frequency\n", +"C=Wo/tan(0.5*W)\n", +"HZ=horner(HS,C*(z-1)/(z+1))\n", +"f=0:120;\n", +"HZ1=abs(horner(HZ,exp(-%i*%pi*f'/120)));\n", +"HS1=abs(horner(HS,(%i*f'/60)));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(f,HZ1);\n", +"plot2d(f,HS1);\n", +"xlabel('Analog Frequency f[kHZ]');\n", +"ylabel('Magnitude');\n", +"xtitle('Notch filter H(S) and digital filter H(z)');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.9: D2D_transformations.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//D2D transformations\n", +"//(a)LP 2 HP transformation\n", +"z=%z;\n", +"HZ=(3*(z+1)^2)/(31*z^2-26*z+7);\n", +"WD=0.5*%pi;WC=0.25*%pi;//Band edges\n", +"a=-cos(0.5*(WD+WC))/cos(0.5*(WD-WC))//Mapping parameter\n", +"HHPZ=horner(HZ,-(z+a)/(1+a*z))\n", +"//(b)LP 2 BP transformation\n", +"W1=0.25*%pi;\n", +"W2=0.75*%pi;\n", +"K=tan(0.5*WD)/tan(0.5*(W2-W1))\n", +"a=-cos(0.5*(W1+W2))/cos(0.5*(W1-W2))//Mapping parameters k,a,A1,A2\n", +"A1=2*K*a/(K+1),A2=(K-1)/(K+1)\n", +"HBPZ=horner(HZ,-(z^2+A1*z+A2)/(A2*z^2+A1*z+1))\n", +"//(c)Lp 2 BS transformation\n", +"w1=3/8*%pi;w2=5/8*%pi;//band edges\n", +"K1=tan(0.5*WD)*tan(0.5*(w2-w1))\n", +"a1=-cos(0.5*(w1+w2))/cos(0.5*(w2-w1))//Mapping parameters k1,a1,A1c,A2c\n", +"A1c=2*a1/(K1+1),A2c=-(K1-1)/(K1+1)\n", +"HBSZ=horner(HZ,(z^2+A1c*z+A2c)/(A2c*z^2+A1c*z+1))\n", +"f=0:1/256:4;\n", +"for i=1:length(f)\n", +"HZ1(i)=horner(HZ,exp(-%i*%pi*f(i)/4));\n", +"end\n", +"HZ1=abs(HZ1);\n", +"HHPZ1=abs(horner(HHPZ,exp(-%i*%pi*f'/4)));\n", +"HBPZ1=abs(horner(HBPZ,exp(-%i*%pi*f'/4)));\n", +"HBSZ1=abs(horner(HBSZ,exp(-%i*%pi*f'/4)));\n", +"a=gca();\n", +"a.x_location='origin';\n", +"plot2d(f,HZ1);\n", +"plot2d(f,HHPZ1);\n", +"plot2d(f,HBPZ1);\n", +"plot2d(f,HBSZ1);\n", +"xlabel('Analog Frequency');\n", +"ylabel('Magnitude');\n", +"xtitle('Digital-to-digital transformations of a low pass filter');" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |