You are on page 1of 20

Let x(t) represent a band limited signal so that X(j ) = 0 for n|. if s n. where is the sampling frequency.

quency. Then x(t) is uniquely determined by its samples. x(n* s The minimum sampling frequency is often referred to as the nyquist sampling rate or nyquist rate. Under sampling: When the sampling rate is less than the nyquist rate, there is overlap of the frequency components and the higher frequency components and the other higher frequency terms of the continuous time signals appear in the low frequency region. This is referred to as aliasing. Over sampling: If the sampling rate is greater then there is a lot of redundancy in the discrete time signal. This is referred to as over sampling.

Matlab Program:

%to verify sampling theoreom %critical sampling f=1400; t=0:1/f:13/f; x=cos(2*pi*400*t)+cos(2*pi*700*t); Xm=abs(fft(x)); n=0:length(x)-1; figure(1) stem(100*n,Xm),grid,zoom; xlabel('frequency'); ylabel('magnitude'); title('criticle sampling'); %under sampling f=700; t=0:1/f:6/f; x=cos(2*pi*400*t)+cos(2*pi*700*t); Xm=abs(fft(x)); k=0:length(x)-1; figure(2) stem(100*k,Xm),grid,zoom; xlabel('frequency'); ylabel('magnitude'); title('under sampling'); %over sampling f=2000; t=0:1/f:27/f; x=cos(2*pi*400*t)+cos(2*pi*700*t); Xm=abs(fft(x)); k=0:length(x)-1; figure(3) stem(100*k,Xm),grid,zoom; xlabel('frequency'); ylabel('magnitude'); title('over sampling');

8. SOLVING DIFFERENTIAL EQUATIONS: Differential equations provide another representation for the input output characteristics of LTI systems. The general form of the linear, constant coefficient differential equation is ak*Dky(t) = bk*Dkx(t) Solving Differential Equations: The Natural Response: It is the output of the system when input is zero. For a continuous time system, natural response yn(t) is the solution to the homogenous equation ak*Dky(t) = 0 The natural response is of the form yn(t) = ci*exp(ri*t) Where ri are the N roots of the systems characteristic equation. Forced response: The forced response is the solution to the differential equation for the given input. The forced response is represented by yp(t) and is similar to the input. MATLAB FUNCTIONS: Dsolve: DSOLVE Symbolic solution of ordinary differential equations. DSOLVE('eqn1','eqn2', ...) accepts symbolic equations representing ordinary differential equations and initial conditions. Several equations or initial conditions may be grouped together, separated by commas, in a single input argument. By default, the independent variable is 't'. The independent variable may be changed from 't' to some other symbolic variable by including that variable as the last input argument. The letter 'D' denotes differentiation with respect to the independent variable, i.e. usually d/dt. A "D" followed by a digit denotes repeated differentiation; e.g., D2 is d^2/dt^2. Any characters immediately following these differentiation operators are taken to be the dependent variables; e.g., D3y denotes the third derivative of y(t). Note that the names of symbolic variables should not contain the letter "D".

Initial conditions are specified by equations like 'y(a)=b' or 'Dy(a) = b' where y is one of the dependent variables and a and b are constants. If the number of initial conditions given is less than the number of dependent variables, the resulting solutions will obtain arbitrary constants, C1, C2, etc. Three different types of output are possible. For one equation and one output, the resulting solution is returned, with multiple solutions to a nonlinear equation in a symbolic vector. For several equations and an equal number of outputs, the results are sorted in lexicographic order and assigned to the outputs. For several equations and a single output, a structure containing the solutions is returned. If no closed-form (explicit) solution is found, an implicit solution is attempted. When an implicit solution is returned, a warning is given. If neither an explicit nor implicit solution can be computed, then a warning is given and the empty sym is returned. In some cases involving nonlinear equations, the output will be an equivalent lower order differential equation or an integral. Examples: dsolve('Dx = -a*x') returns ans = C1*exp(-a*t) x = dsolve('Dx = -a*x','x(0) = 1','s') returns x = exp(-a*s) y = dsolve('(Dy)^2 + y^2 = 1','y(0) = 0') returns y= [ sin(t)] [ -sin(t)] S = dsolve('Df = f + g','Dg = -f + g','f(0) = 1','g(0) = 2') returns a structure S with fields S.f = exp(t)*cos(t)+2*exp(t)*sin(t) S.g = -exp(t)*sin(t)+2*exp(t)*cos(t) dsolve('Dy = y^2*(1-y^2)') returns Warning:Explicit solution could not be found; implicit solution returned.

ans = t+1/2*log(y-1)-1/2*log(y+1)+1/y+C1=0 dsolve('Df = f + sin(t)', 'f(pi/2) = 0') dsolve('D2y = -a^2*y', 'y(0) = 1, Dy(pi/a) = 0') S = dsolve('Dx = y', 'Dy = -x', 'x(0)=0', 'y(0)=1') S = dsolve('Du=v, Dv=w, Dw=-u','u(0)=0, v(0)=0, w(0)=1') w = dsolve('D3w = -w','w(0)=1, Dw(0)=0, D2w(0)=0') y = dsolve('D2y = sin(y)'); pretty(y) Simple: SIMPLE Search for simplest form of a symbolic expression or matrix. SIMPLE(S) tries several different algebraic simplifications of S, displays any which shorten the length of S's representation, and returns the shortest. S is a SYM. If S is a matrix, the result represents the shortest representation of the entire matrix, which is not necessarily the shortest representation of each individual element. [R,HOW] = SIMPLE(S) does not display intermediate simplifications, but returns the shortest found, as well as a string describing the particular simplification. R is a SYM. HOW is a string. Examples: S cos(x)^2+sin(x)^2 2*cos(x)^2-sin(x)^2 cos(x)^2-sin(x)^2 cos(x)+(-sin(x)^2)^(1/2) cos(x)+i*sin(x) (x+1)*x*(x-1) x^3+3*x^2+3*x+1 cos(3*acos(x)) syms x y positive log(x) + log(y) Pretty: PRETTY Pretty print a symbolic expression. PRETTY(S) prints the symbolic expression S in a format that resembles type-set mathematics. R How

1 simplify 3*cos(x)^2-1 simplify cos(2*x) combine(trig) cos(x)+i*sin(x) radsimp exp(i*x) convert(exp) x^3-x combine(trig) (x+1)^3 factor 4*x^3-3*x expand log(x*y) combine

Matlab Program: disp('Solution of differential equation:'); disp('Enter the data in the following format'); disp('Include all data within single quotes'); disp('D2y-3*Dy+4*y=0'); disp('x'); equation=input('Enter the homogenous differential equation'); ic=input('Enter the initial conditions:'); y=diffsoln(equation,ic); %function M-file to be wriiten in a new file with the same name as the function %diffsoln.m function y=diffsoln(equation,ic); y=dsolve(equation,ic); y=simple(y); disp('Solution of the differential equation is:'); pretty(y);

9. IMPULSE RESPONSE Consider the system which is represented by the difference equation a0*y[n] + a1*y[n-1] + +am*y[n-m] = b0*x[n] + b1*x[n-1] + b2*x[n-2] ++bk*x[n-k] taking the Z-transform on both sides a0*Y[z] + a1*z-1*Y[z] +..+am*z-m*Y[z] = b0*X[z] + b1*z-1*X[z] +.bk*z-k*X[z] This equation gives the transfer function as H[z] = Y[z]/X[z] H[z] = (b0 + b1*z-1 +.bk*z-k)/(a0 + a1*z-1 + .am*z-m) Thus the transfer function is defined in terms of the coefficients of the numerator polynomial and the denominator polynomial. The inverse Z-transform of the transfer function gives the impulse response of the system. MATLAB FUNCTIONS: Filter: FILTER One-dimensional digital filter. Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B to create the filtered data Y. The filter is a "Direct Form II Transposed" implementation of the standard difference equation: a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

If a(1) is not equal to 1, FILTER normalizes the filter coefficients by a(1). FILTER always operates along the first non-singleton dimension, namely dimension 1 for column vectors and non-trivial matrices, and dimension 2 for row vectors. [Y,Zf] = FILTER(B,A,X,Zi) gives access to initial and final conditions, Zi and Zf, of the delays. Zi is a vector of length MAX(LENGTH(A),LENGTH(B))-1, or an array with the leading dimension of size MAX(LENGTH(A),LENGTH(B))-1 and with remaining dimensions matching those of X. FILTER(B,A,X,[],DIM) or FILTER(B,A,X,Zi,DIM) operates along the dimension DIM.

Matlab Program: % to find the impulse response of a system given the difference equation N=input('the length of the impulse response:'); b=input('the coefficients of the input terms x(n) :'); a=input('the coefficients of the output terms y(n):'); x=[1 zeros(1,N-1)]; h=filter(b,a,x); figure(1);stem(h); disp('the impulse response of the system'); disp(h); %to find the response of the system for the given input xi=input('enter the input sequence:'); y=conv(xi,h); disp('the response of the system:'); disp(y); figure(2); stem(y)

10. CONVOLUTION OF TWO FINITE LENGTH SEQUENCES(LINEAR AND CIRCULAR) Impulse function: The continuous time version of the unit impulse, commonly denoted by (t), is defined by the following pair of relations (t) and (t) = by 1, for n = 0; 0, for n != 0 Impulse response of a linear time-invariant system: Consider the product signal, x[n]*[n] = x[0]*[n] we can generalize this relationship as x[n]*[n-k] = x[k]*[n-k], i.e. [n] = 1 The discrete-time version of the impulse response commonly denoted by [n], is defined = 0 for t !=0

we can express x[n] as a weighted sum of impulse functions as shown in the equation given below x[n] = + x[-2]* [n+2] + x[-1]* [n+1] + x[0]* [n] + x[1]* [n-1] +. Let H denote the system to which the input is applied. Then using the above equation to represent the input x[n] to the system results in the output. y[n] = H{x[k]* [n-k]}

as the system is linear and time invariant, y[n] = = x[k]*H{ [n-k]} x[k]*h[n-k]

where h[n] is the response of the system to the unit impulse. This operation is defined as linear convolution. Circular convolution: A transform of the form y[n] = x[k]*h[(n-k)]N

is defined as circular convolution. The index [(n-k)]N is a modulo N shift operator and occurs due to the periodic properties of the Discrete Fourier Transform.

MATLAB FUNCTIONS CONV: CONV Convolution and polynomial multiplication. C = CONV(A, B) convolves vectors A and B. The resulting vector is length LENGTH(A)+LENGTH(B)-1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials. Class support for inputs A,B: float: double, single FFT: FFT Discrete Fourier transform. FFT(X) is the discrete Fourier transform (DFT) of vector X. For matrices, the FFT operation is applied to each column. For N-D arrays, the FFT operation operates on the first non-singleton dimension.

FFT(X,N) is the N-point FFT, padded with zeros if X has less than N points and truncated if it has more. FFT(X,[],DIM) or FFT(X,N,DIM) applies the FFT operation across the dimension DIM. For length N input vector x, the DFT is a length N vector X, with elements N X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N. n=1 The inverse DFT (computed by IFFT) is given by N x(n) = (1/N) sum X(k)*exp( j*2*pi*(k-1)*(n-1)/N), 1 <= n <= N. k=1 IFFT: IFFT Inverse discrete Fourier transform. IFFT(X) is the inverse discrete Fourier transform of X. IFFT(X,N) is the N-point inverse transform. IFFT(X,[],DIM) or IFFT(X,N,DIM) is the inverse discrete Fourier transform of X across the dimension DIM. IFFT(..., 'symmetric') causes IFFT to treat F as conjugate symmetric along the active dimension. This option is useful when F is not exactly conjugate symmetric merely because of round-off error. See the reference page for the specific mathematical definition of this symmetry. IFFT(..., 'nonsymmetric') causes IFFT to make no assumptions about the symmetry of F. Matlab Program: % convolution of two given sequences (linear, circular) disp(' convolution of sequences x1(n) and x2(n)'); x1=input ('enter the first sequences x1(n):'); x2=input ('enter the second sequences x2(n):'); ylin1=conv(x1,x2); disp('') disp(' the linear convolution of x1(n) and x2(n) using conv()');

disp('yeilds the result:'); disp(ylin1); disp('') %linear convolution using fft() and ifft() N=length(x1)+ length(x2)-1; X1=fft(x1,N); X2=fft(x2,N); Y1=X1.*X2; ylin2=ifft(Y1); disp('the linear - convolution of x1(n) and x2(n) using fft()') disp('and ifft() yeilds the result:'); disp(ylin2); disp('') %circular convolution P=max(length(x1),length(x2)); X1=fft(x1,P); X2=fft(x2,P); Yc=X1.*X2; Yc=ifft(Yc); disp('the circular convolution of x1(n) and x2(n) using fft()') disp('and ifft() yeilds the result:'); disp(Yc);

11. DISCRETE FOURIER TRANSFORM AND INVERSE FOURIER TRANSFORM: Frequency Domain Sampling And Reconstruction Of Discrete Signals: Consider an aperiodic discrete-time signal. Consider the Fourier transform of the signal X( = x[n]*exp(-j* n) k*n/N we obtain

Suppose we sample X( periodically in frequency, if we evaluate at X( k*n/N = x[n]*exp(-j*( k*n/N)n)

The inverse of the sampled Frequency response gives a signal xp[n] which is a periodic repetition of the original aperiodic signal, repeated with a period of N. This transform is called the DFT on x[n]. Matlab Program: disp('Calculation of dft'); disp(''); x=input('enter the sequence x(n) '); x=fft(x); disp('the dft of x(n)is '); disp(x); disp(' Press any key mag and ph plots'); pause; n= length(x); k=0:n-1; Xm= abs(x); subplot(2,1,1);stem(k,Xm),grid,zoom; title('Mag'); Xp =angle(x); subplot(2,1,2);stem(k,Xp),grid,zoom; title('phase plot'); xlabel('k'),ylabel('phase(radians)'); disp(''); disp('calc of IDFT'); disp(''); Y=input('enter the DFT Y(k)'); y=ifft(Y); disp('The IDFT of Y(k)'); disp(y);

12. DESIGN OF IIR FILTERS IIR filter design by impulse invariance: In the impulse invariance method, our objective is to design an IIR filter having unit sample response h[n] that is the sampled version of the impulse response of the analog filter. h[n] = h(nT) T sampling interval

for an analog filter with frequency response Ha[F], the digital filter with unit sample response h[n] = ha(nT) has the frequency response H (f) Or H( ) = Or H ( T) = For the Z-transform H(z)|z=exp(sT) = 1/THa(s j* k/T) Where H(z) = h[n]z-n H(z)|z=exp(sT) = h[n]exp (-snT) Lets consider the mapping z = exp (sT) Clearly we must have r = exp ( T) exp ( T) On the assumption that the poles of the analog filter are distinct, we can write Ha(s) = ck /(s pk) ha (t) = ck * exp (pkt) if this signal is sampled periodically at t = nT, the resulting IIR filter becomes h[n] = ha(nT) = ck * exp (pk *n*T) The resulting IIR filter becomes H (z) = ck (exp (pk*T) * z-1) n = ck/(1- exp (pk*T)* z-1) The filter has poles at zk = exp (pk*T) Chebyshev filters: There are two types of Chebyshev filters. Type I and type II. Type I filters are all pole filters that exhibit equiripple in pass band and a monotonic characteristic in the stop band. Type II contain poles and zeroes and exhibit a monotonic behavior in the pass band and an equiripple behavior in stop band. The magnitude of the frequency response is given as |H ( | = 1/ (1+
2

FsHa [(f-k)*Fs] FsHa [( - k)*Fs] 1/THa ( - k/T)

TN2 (

))

MATLAB FUNCTIONS: Cheb1ord:

CHEB1ORD Chebyshev Type I filter order selection. [N, Wn] = CHEB1ORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital Chebyshev Type I filter that loses no more than Rp dB in the passband and has at least Rs dB of attenuation in the stopband. Wp and Ws are the passband and stopband edge frequencies, normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For example, Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1 Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7] CHEB1ORD also returns Wn, the Chebyshev natural frequency to use with CHEBY1 to achieve the specifications. [N, Wn] = CHEB1ORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which case Wp and Ws are in radians/second. Cheby1: CHEBY1 Chebyshev Type I digital and analog filter design. [B,A] = CHEBY1(N,R,Wn) designs an Nth order lowpass digital Chebyshev filter with R decibels of peak-to-peak ripple in the passband. CHEBY1 returns the filter coefficients in length N+1 vectors B (numerator) and A (denominator). The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. Use R=0.5 as a starting point, if you are unsure about choosing R. If Wn is a two-element vector, Wn = [W1 W2], CHEBY1 returns an order 2N bandpass filter with passband W1 < W < W2. [B,A] = CHEBY1(N,R,Wn,'high') designs a highpass filter. [B,A] = CHEBY1(N,R,Wn,'low') designs a lowpass filter. [B,A] = CHEBY1(N,R,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. When used with three left-hand arguments, as in [Z,P,K] = CHEBY1(...), the zeros and poles are returned in length N column vectors Z and P, and the gain in scalar K. When used with four left-hand arguments, as in [A,B,C,D] = CHEBY1(...), state-space matrices are returned. CHEBY1(N,R,Wn,'s'), CHEBY1(N,R,Wn,'high','s') and CHEBY1(N,R,Wn,'stop','s') design analog Chebyshev Type I filters. In this case, Wn is in [rad/s] and it can be greater than 1.0. Buttord:

BUTTORD Butterworth filter order selection. [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital Butterworth filter that loses no more than Rp dB in the passband and has at least Rs dB of attenuation in the stopband. Wp and Ws are the passband and stopband edge frequencies, normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For example, Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1 Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7] BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency") to use with BUTTER to achieve the specifications. [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which case Wp and Ws are in radians/second. When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in BUTTORD. Butter: UTTER Butterworth digital and analog filter design. [B,A] = BUTTER(N,Wn) designs an Nth order lowpass digital Butterworth filter and returns the filter coefficients in length N+1 vectors B (numerator) and A (denominator). The coefficients are listed in descending powers of z. The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. If Wn is a two-element vector, Wn = [W1 W2], BUTTER returns an order 2N bandpass filter with passband W1 < W < W2. [B,A] = BUTTER(N,Wn,'high') designs a highpass filter. [B,A] = BUTTER(N,Wn,'low') designs a lowpass filter. [B,A] = BUTTER(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. When used with three left-hand arguments, as in [Z,P,K] = BUTTER(...), the zeros and poles are returned in length N column vectors Z and P, and the gain in scalar K. When used with four left-hand arguments, as in [A,B,C,D] = BUTTER(...), state-space matrices are returned. Butter: BUTTER(N,Wn,'s'), BUTTER(N,Wn,'high','s') and BUTTER(N,Wn,'stop','s') design analog Butterworth filters. In this case, Wn is in [rad/s] and it can be greater than 1.0.

Consider the frequency response of an ideal low pass filter. H( ) = = 1 0 for | |<= for | |>=

c c

The impulse response of this filter is h [n] = =


c

for n = 0
c

sin ( cn

n) for n != 0

The resulting function is non causal and infinite. The ideal response is truncated by a window. Different types of windows are in use. In this example we use a Hamming window with a function w [n] = 0.54 0.46*cos ((2**n)/(M-1))

MATLAB FUNCTIONS: Hamming: HAMMING Hamming window. HAMMING(N) returns the N-point symmetric Hamming window in a column vector. HAMMING(N,SFLAG) generates the N-point Hamming window using SFLAG window sampling. SFLAG may be either 'symmetric' or 'periodic'. By default, a symmetric window is returned. Fir1: FIR1 FIR filter design using the window method. B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. The filter B is real and has linear phase. The normalized gain of the filter at Wn is -6 dB. B = FIR1(N,Wn,'high') designs an N'th order highpass filter. You can also use B = FIR1(N,Wn,'low') to design a lowpass filter. If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter with passband W1 < W < W2. You can also specify B = FIR1(N,Wn,'bandpass'). If Wn = [W1 W2], B = FIR1(N,Wn,'stop') will design a bandstop filter.

If Wn is a multi-element vector, Wn = [W1 W2 W3 W4 W5 ... WN], FIR1 returns an order N multiband filter with bands 0 < W < W1, W1 < W < W2, ..., WN < W < 1. B = FIR1(N,Wn,'DC-1') makes the first band a passband. B = FIR1(N,Wn,'DC-0') makes the first band a stopband. B = FIR1(N,Wn,WIN) designs an N-th order FIR filter using the N+1 length vector WIN to window the impulse response. If empty or omitted, FIR1 uses a Hamming window of length N+1. For a complete list of available windows, see the help for the WINDOW function. KAISER and CHEBWIN can be specified with an optional trailing argument. For example, B = FIR1(N,Wn,kaiser(N+1,4)) uses a Kaiser window with beta=4. B = FIR1(N,Wn,'high',chebwin(N+1,R)) uses a Chebyshev window with R decibels of relative sidelobe attenuation. For filters with a gain other than zero at Fs/2, e.g., highpass and bandstop filters, N must be even. Otherwise, N will be incremented by one. In this case the window length should be specified as N+2. By default, the filter is scaled so the center of the first pass band has magnitude exactly one after windowing. Use a trailing 'noscale' argument to prevent this scaling, e.g. B = FIR1(N,Wn,'noscale'), B = FIR1(N,Wn,'high','noscale'), B = FIR1(N,Wn,wind,'noscale'). You can also specify the scaling explicitly, e.g. FIR1(N,Wn,'scale'), etc.

Matlab Program: %design of an IIR filter Fp=input ('Enter the passband edge freq wp in hertz'); Fb=input ('Enter the stopband edge freq ws in radians'); rp=input('Enter passband ripple in db '); rs=input(' Enter stopband attenuation in db '); Fs=8000; %normalize frequencies wp = 2*Fp/Fs; ws = 2*Fb/Fs; [N,wn]=cheb1ord(wp,ws,rp,rs); disp('The order of the filter is '); disp(N); [b,a] =cheby1(N,rp,wn); disp(' The digital filter numerator coefficients are '); disp(b); disp(' The digital filter denominator coefficients are '); disp(a); freqz(b,a); % low pass FIR filter using hamming window wp=input('enter wp in radians'); wb=input('enter wb in radians'); wt=wb-wp; nl=ceil(8*pi/wt); N=nl+rem(nl-1,2); disp('the order of the filter based hamming window is'); disp(N); %define window function wn=hamming(N); %to find the impulse response we=wp/pi; h=fir1(N-1,we,wn); figure(1); freqz(h); figure(2); n=0:N-1; stem(n,h); title('impulse response of FIR filter'); xlabel('n'); ylabel('h(n)');