{ "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 }