Dsp Lab Manual

download Dsp Lab Manual

of 171

Transcript of Dsp Lab Manual

EC246 DIGITAL SIGNAL PROCESSING LAB

FLOWCHART:

AIM:

Write a program in MATLAB to generate the following waveforms (Discrete Time signal and Continuous Time signal)1. Unit Impulse sequence,

2. Unit step sequence,3. Unit Ramp sequence,4. Sinusoidal sequence,

5. Exponential sequence,

6. Random sequence,1. Pulse signal,

2. Unit step signal

3. Ramp signal

4. Sinusoidal signal,

5. Exponential signal,

6. Random signal

APPARATUS REQUIRED:

Pentium 4 Processor, MATLAB softwareTHEORY:

Real signals can be quite complicated. The study of signals therefore starts with the analysis of basic and fundamental signals. For linear systems, a complicated signal and its behaviour can be studied by superposition of basic signals. Common basic signals are:

Discrete Time signals:

Unit impulse sequence. Unit step sequence. Unit ramp sequence. Sinusoidal sequence. .

Exponential sequence. x(n) = A an, where A and a are constant. Continuous time signals:

Unit impulse signal.

Unit step signal. Unit ramp signal.

Sinusoidal signal. .

Exponential signal. , where A and a are constant. LIBRARY FUNCTIONS:

clc:

CLC Clear command window.

CLC clears the command window and homes the cursor.

clear all:

CLEAR Clear variables and functions from memory. CLEAR removes all variables from the workspace.CLEAR VARIABLES does the same thing.

close all:

CLOSE Close figure.CLOSE, by itself, closes the current figure window.

CLOSE ALL closes all the open figure windows.

exp:

EXP Exponential.

EXP(X) is the exponential of the elements of X, e to the X.

input:

INPUT Prompt for user input.

R = INPUT('How many apples') gives the user the prompt in the text string and then waits for input from the keyboard. The input can be any MATLAB expression, which is evaluated,using the variables in the current workspace, and the result returned in R. If the user presses the return key without entering anything, INPUT returns an empty matrix.

linspace:

LINSPACE Linearly spaced vector.

LINSPACE(X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2.

rand:

The rand function generates arrays of random numbers whose elements are uniformly distributed in the interval (0,1).ones:

ONES(N) is an N-by-N matrix of ones.

ONES(M,N) or ONES([M,N]) is an M-by-N matrix of ones.

zeros:

ZEROS(N) is an N-by-N matrix of Zeros.

ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros

plot:

PLOT Linear plot.

PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up.

subplot:

SUBPLOT Create axes in tiled positions.

H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc.

stem:

STEM Discrete sequence or "stem" plot.

STEM(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value.

STEM(X,Y) plots the data sequence Y at the values specified in X.

title:

TITLE Graph title.

TITLE('text') adds text at the top of the current axis.

xlabel:

XLABEL X-axis label.

XLABEL('text') adds text beside the X-axis on the current axis.

ylabel:

YLABEL Y-axis label.

YLABEL('text') adds text beside the Y-axis on the current axis.

ALGORITHM/PROCEDURE:

1. Start the program

2. Get the inputs for signal generation3. Use the appropriate library function

4. Display the waveform

Source code :

%WAVE FORM GENERATION%CT SIGNAL%UNIT IMPULSEclc;clear all;close all;t1=-3:1:3;x1=[0,0,0,1,0,0,0];subplot(2,3,1);plot(t1,x1);xlabel('time');ylabel('Amplitude');title('Unit impulse signal');%UNIT STEP SIGNALt2=-5:1:25;x2=[zeros(1,5),ones(1,26)];subplot(2,3,2);plot(t2,x2);xlabel('time');ylabel('Amplitude');title('Unit step signal');%EXPONENTIAL SIGNALa=input('Enter the value of a:');t3=-10:1:20;x3=exp(-1*a*t3);subplot(2,3,3);plot(t3,x3);xlabel('time');ylabel('Amplitude');title('Exponential signal');%UNIT RAMP SIGNALt4=-10:1:20;x4=t4;subplot(2,3,4);plot(t4,x4);xlabel('time');ylabel('Amplitude');title('Unit ramp signal');%SINUSOIDAL SIGNALA=input('Enter the amplitude:');f=input('Enter the frequency:');t5=-10:1:20;x5=A*sin(2*pi*f*t5);subplot(2,3,5);plot(t5,x5)xlabel('time');ylabel('Amplitude');title('Sinusoidal signal');%RANDOM SIGNALt6=-10:1:20;x6=rand(1,31);subplot(2,3,6);plot(t6,x6);xlabel('time');ylabel('Amplitude');title('Random signal');%WAVE FORM GENERATION%DT SIGNAL%UNIT IMPULSEclc;clear all;close all;n1=-3:1:3;x1=[0,0,0,1,0,0,0];subplot(2,3,1);stem(n1,x1);xlabel('time');ylabel('Amplitude');title('Unit impulse signal');%UNIT STEP SIGNALn2=-5:1:25;x2=[zeros(1,5),ones(1,26)];subplot(2,3,2);stem(n2,x2);xlabel('time');ylabel('Amplitude');title('Unit step signal');%EXPONENTIAL SIGNALa=input('Enter the value of a:');n3=-10:1:20;x3=power(a,n3);subplot(2,3,3);stem(n3,x3);xlabel('time');ylabel('Amplitude');title('Exponential signal');%UNIT RAMP SIGNALn4=-10:1:20;x4=n4;subplot(2,3,4);stem(n4,x4);xlabel('time');ylabel('Amplitude');title('Unit ramp signal');%SINUSOIDAL SIGNALA=input('Enter the amplitude:');f=input('Enter the frequency:');n5=-10:1:20;x5=A*sin(2*pi*f*n5);subplot(2,3,5);stem(n5,x5);xlabel('time');ylabel('Amplitude');title('Sinusoidal signal');%RANDOM SIGNALn6=-10:1:20;x6=rand(1,31);subplot(2,3,6);stem(n6,x6);xlabel('time');ylabel('Amplitude');title('Random signal');CONTINUOUS TIME:

DISCRETE TIME :

OUTPUT WAVEFORM(CONTINUOUS TIME):

OUTPUT WAVEFORM (DISCRETE TIME):

RESULT: The program to generate various waveforms is written, executed and the output is verified. FLOWCHART:

AIM:

Write a program in MATLAB to study the basic operations on the Discrete time signals. (Operation on dependent variable (amplitude manipulation) and Operation on independent variable (time manipulation)).

APPARATUS REQUIRED:

Pentium 4 Processor, MATLAB software

THEORY:

Let x(n) be a sequence with finite length.

1. Amplitude manipulation

Amplitude scaling:y[n] =ax[n], where a is a constant.

If a > 1, then y[n] is amplified sequence

If a < 1, then y[n] is attenuated sequence

If a = - 1, then y[n] is amplitude reversal sequence

Offset the signal:y[n] =a+x[n], where a is a constant

Two signals x1[n] and x2[n] can also be added and multiplied: By adding the values y1[n]= x1[n] + x2[n] at each corresponding sample and by multiplying the values y2[n]= x1[n] X x2[n] at each corresponding sample.2. Time manipulation

Time scaling:

y[n]=x[an], where a is a constant.

Time shifting:

y[n]=x[n - (], where ( is a constant.

Time reflection (folding):y[n]=x[-n]Arithmetic Operations

* Matrix multiplication

.* Array multiplication (element-wise)LIBRARY FUNCTIONS:

date Current date as date string.

S = date returns a string containing the date in dd-mmm-yyyy format

tic & toc Start a stopwatch timer.

The sequence of commands

TIC, operation, TOC

prints the number of seconds required for the operation.

Fprintf

Write formatted data to file. The special formats \n,\r,\t,\b,\f can be used to produce linefeed, carriage return, tab, backspace, and formfeed characters respectively.

Use \\ to produce a backslash character and %% to produce the percent character.

ALGORITHM/PROCEDURE:

1. Start the program

2. Get the input for signal manipulation

3. Use the appropriate library function

4. Display the waveform

Source code :clc;clear all;close all;%operations on the amplitude of signalx=input('Enter input sequence:');a=input('Enter amplification factor:');b=input('Enter attenuation factor:');c=input('Enter amplitude reversal factor:');y1=a*x;y2=b*x;y3=c*x;n=length(x);subplot(2,2,1);stem(0:n-1,x);xlabel('time');ylabel('amplitude');title('Input signal');subplot(2,2,2);stem(0:n-1,y1);xlabel('time');ylabel('Amplitude');title('Amplified signal');subplot(2,2,3);stem(0:n-1,y2);xlabel('time');ylabel('Amplitude');title('Attenuated signal');subplot(2,2,4);stem(0:n-1,y3);xlabel('time');ylabel('Amplitude');title('Amplitude reversal signal');%scalar additiond=input('Input the scalar to be added:');y4=d+x;figure(2);stem(0:n-1,y4);xlabel('time');ylabel('Amplitude');title('Scalar addition signal');clc;clear all;close all;%Operations on the independent variable%Time shifting of the independent variablex=input('Enter the input sequence:');n0=input('Enter the +ve shift:');n1=input('Enter the -ve shift:');l=length(x);subplot(2,2,1);stem(0:l-1,x);xlabel('time');ylabel('Amplitude');title('Input signal');i=n0:l+n0-1;j=n1:l+n1-1;subplot(2,2,2);stem(i,x);xlabel('time');ylabel('Amplitude');title('Positive shifted signal');subplot(2,2,3);stem(j,x);xlabel('time');ylabel('Amnplitude');title('Negative shifted signal');%Time reversalsubplot(2,2,4);stem(-1*(0:l-1),x);xlabel('time');ylabel('Amplitude');title('Time reversal signal');clc;clear all;close all;%Arithmetic operations on signals%Addition and multiplication of two signalsx1=input('Enter the sequence of first signal:');x2=input('Enter the sequence of second signal:');l1=length(x1);l2=length(x2);subplot(2,2,1);stem(0:l1-1,x1);xlabel('time');ylabel('Amplitude');title('Input sequence 1');subplot(2,2,2);stem(0:l2-1,x2);xlabel('time');ylabel('Amplitude');title('Input sequence 2');if l1>l2 l3=l1-l2; x2=[x2,zeros(1,l3)]; y1=x1+x2; subplot(2,2,3); stem(0:l1-1,y1);xlabel('time');ylabel('Amplitude');title('Addition of two signals');y2=x1.*x2; subplot(2,2,4); stem(0:l1-1,y2);xlabel('time');ylabel('Amplitude');title('Multiplication of two signals');endif l2>l1 l3=l2-l1; x1=[x1,zeros(1,l3)]; y1=x1+x2; subplot(2,2,3); stem(0:l2-1,y1);xlabel('time');ylabel('Amplitude');title('Addition of two signals');y2=x1.*x2; subplot(2,2,4); stem(0:l2-1,y2);xlabel('time');ylabel('Amplitude');title('Multiplication of two signals');else y1=x1+x2; subplot(2,2,3); stem(0:l1-1,y1);xlabel('time');ylabel('Amplitude');title('Addition of two signals');y2=x1.*x2; subplot(2,2,4); stem(0:l1-1,y2); xlabel('time');ylabel('Amplitude');title('Multiplication of two signals');endoperations on the amplitude of signal :

Time shifting of the independent variable :

Addition and multiplication of two signals :

OUTPUT (operations on the amplitude of signal ):

OUTPUT(Time shifting of the independent variable) :

OUTPUT(Addition and multiplication of two signals) :

RESULT:

The program to perform various operations on discrete time signal is written, executed and the output is verified

FLOWCHART:

AIM:

To check for linearity, Causality and stability of various systems given bellow:

Linearity: System1 n.X(n), System2 An.X2(n)+B System3: Log (X),sin(x),5X(n) etc

Causality: System1 U(-n) System2 X(n-4)+U(n+5)

Stability: System1 Z / (Z2 + 0.5 Z+1)

APPARATUS REQUIRED:

Pentium 4 Processor, MATLAB software

THEORY:

LINEARITY:

The response of the system to a weighted sum of signals is equal to the corresponding weighted sum of the responses (outputs) of the system to each of the individual input signals.

METHODS OF PROOF:

Individual inputs are applied and the weighted sum of the outputs is taken. Then the weighted sum of signals input is applied to the system and the two outputs are checked to see if they are equal.

CAUSALITY:

A system is said to be causal, if the output of the system at any time n(y(n)) depends only on the present and past inputs and past outputs [x(n),x(n-1).y(n-1),..]

But does not depend on future inputs [x (n+1),x(n+2),..]

y(n) = F[ x(n),x(n-1),x(n-2).] F[ ] Arbitrary function.

METHODS OF PROOF:

1. If the difference equation is given, the arguments of the output y (n) are compared with the arguments (time instant) of the input signals. In the case of only present and past inputs, the system is causal. If future inputs are present then the system is non-causal.

2. If the impulse response is given, then it is checked whether all the values of h (n) for negative values of n are zero. (i.e.) if h(n)=0, for > that appears in the Command window.

3. Write the program in the Edit window and save it in M-file

4. Run the program

5. Enter the input in the command window

6. The result is displayed in the Command window and the graphical output is displayed in the Figure Window

Source code :1clc;

clear all;

close all;

%Properties of DT Systems(Linearity)

%y(n)=[x(n)]^2+B;

x1=input('Enter first input sequence:');

n=length(x1);

x2=input('Enter second input sequence:');

a=input('Enter scaling constant(a):');

b=input('Enter scaling constant(b):');

B=input('Enter scaling constant(B):');

y1=power(x1,2)+B;

y2=power(x2,2)+B;

rhs=a*y1+b*y2;

x3=a*x1+b*x2;

lhs=power(x3,2)+B;

subplot(2,2,1);

stem(0:n-1,x1);

xlabel('Time');

ylabel('Amplitude');

title('First input sequence');

subplot(2,2,2);

stem(0:n-1,x2);

xlabel('Time');

ylabel('Amplitude');

title('Second input sequence');

subplot(2,2,3);

stem(0:n-1,lhs);

xlabel('Time');

ylabel('Amplitude');

title('LHS');

subplot(2,2,4);

stem(0:n-1,rhs);

xlabel('Time');

ylabel('Amplitude');

title('RHS');

if(lhs==rhs)

display('system is linear');

else

display('system is non-linear');

end;

Source code :2clc;

clear all;

close all;

%Properties of DT Systems(Linearity)

%y(n)=x(n);

x1=input('Enter first input sequence:');

x2=input('Enter second input sequence:');

a=input('Enter scaling constant(a):');

b=input('Enter scaling constant(b):');

subplot(2,2,1);

stem(x1);

xlabel('time');

ylabel('Amplitude');

title('First signal');

subplot(2,2,2);

stem(x2);

xlabel('time');

ylabel('Amplitude');

title('Second signal');

y1=x1;

y2=x2;

rhs=a*y1+b*y2;

x3=a*x1+b*x2;

lhs=x3;

if(lhs==rhs)

display('system is linear');

else

display('system is non-linear');

end;

subplot(2,2,3);

stem(lhs);

xlabel('time');

ylabel('Amplitude');

title('L.H.S');

subplot(2,2,4);

stem(rhs);

xlabel('time');

ylabel('Amplitude');

title('R.H.S');

Source code :3

clc;

clear all;

close all;

%Properties of DT Systems(Time Invariance)

%y(n)=x(n);

x1=input('Enter input sequence x1:');

n0=input('Enter shift:');

x2=[zeros(1,n0),x1];

y1=x1;

y2=x2;

y3=[zeros(1,n0),y1];

if(y2==y3)

display('system is time invariant');

else

display('system is time variant');

end;

subplot(2,2,1);

stem(x1);

xlabel('time');

ylabel('Amplitude');

title('Input signal');

subplot(2,2,2);

stem(x2);

xlabel('time');

ylabel('Amplitude');

title('Signal after shift');

subplot(2,2,3);

stem(y2);

xlabel('time');

ylabel('Amplitude');

title('L.H.S');

subplot(2,2,4);

stem(y3);

xlabel('time');

ylabel('Amplitude');

title('R.H.S');

Source code :4

clc;

clear all;

close all;

%Properties of DT Systems(Time Invariance)

%y(n)=n*[x(n)];

x1=input('Enter input sequence x1:');

n1=length(x1);

for n=1:n1

y1(n1)=n.*x1(n);

end;

n0=input('Enter shift:');

x2=[zeros(1,n0),x1];

for n2=1:n1+n0

y2(n2)=n2.*x2(n2);

end;

y3=[zeros(1,n0),y1];

if(y2==y3)

display('system is time invariant');

else

display('system is time variant');

end;

subplot(2,2,1);

stem(x1);

xlabel('time');

ylabel('Amplitude');

title('Input signal');

subplot(2,2,2);

stem(x2);

xlabel('time');

ylabel('Amplitude');

title('Signal after shift');

subplot(2,2,3);

stem(y2);

xlabel('time');

ylabel('Amplitude');

title('L.H.S');

subplot(2,2,4);

stem(y3);

xlabel('time');

ylabel('Amplitude');

title('R.H.S');

Source code :5clc;

clear all;

close all;

%Properties of DT Systems(Causality)

%y(n)=x(-n);

x1=input('Enter input sequence x1:');

n1=input('Enter lower limit n1:');

n2=input('Enter lower limit n2:');

flag=0;

for n=n1:n2

arg=-n;

if arg>n;

flag=1;

end;

end;

if(flag==1)

display('system is causal');

else

display('system is non-causal');

end;

Source code :6disp('stability');

nr=input('input the numerator coefficients:');

dr=input('input the denominator coefficients:');

z=tf(nr,dr,1);

[r,p,k]=residuez(nr,dr);

figure

zplane(nr,dr);

if abs(p) 1, the process is called interpolation and results in a sequence with higher sampling rate. If R< 1, the process is called decimation and results in a sequence with lower sampling rate.

DOWNSAMPLE AND DECIMATION:

Down sampling operation by an integer factor M (M>1) on a sequence x[n] consists of keeping every Mth sample of x[n] and removing M-1 in between samples, generating an output sequence y[n] according to the relation

y [n] = x[nM]

y [n] sampling rate is 1/M that of x[n]

If we reduce the sampling rate, the resulting signal will be an aliased version of x[n]. To avoid aliasing, the bandwidth of x[n] must be reduced to Fmax =Fx/2 or (max = /M. The input sequence is passed through LPF or an antialiasing filter before down sampling.

x [n] - y[n]

UPSAMPLE AND INTERPOLATION:

Upsampling by an integer factor L (L > 1) on a sequence x[n] will insert (L1) equidistant samples between an output sequence y[n] according to the relation

x[n/L],n = 0, (1, (2 .

y[n] = 0,

otherwise

The sampling rate of y[n] is L times that of x[n]. The unwanted images in the spectra of sampled signal must be removed by a LPF called anti-imaging filter. The input sequence is passed through an anti-imaging filter after up sampling.

x[n]

y[n]

SAMPLING RATE CONVERSION BY A RATIONAL FACTOR I/O:

We achieve this conversion, by first performing interpolation by the factor I and then decimating the output of interpolator by the factor D, interpolation has to be performed before decimation to obtain the new rational sampling rate.

x[n]

y[n]

LIBRARY FUNCTIONS:

resample: Changes sampling rate by any rational factor.y = resample (x,p,q) resamples the sequence in vector x at p/q times the original sampling rate, using a polyphase filter implementation. p and q must be positive integers. The length of y is equal to ceil (length(x)*p/q).

interp: Increases sampling rate by an integer factor (interpolation)

y = interp (x,r) increases the sampling rate of x by a factor of r. The interpolated vector y is r times longer than the original input x. interp performs low pass interpolation by inserting zeros into the original sequence and then applying a special low pass filter.

upsample: Increases the sampling rate of the input signal

y = upsample(x,n) increases the sampling rate of x by inserting n-1 zeros between samples. The upsampled y has length(x)*n samples

decimate: Decreases the sampling rate for a sequence (decimation).

y = decimate (x, r) reduces the sample rate of x by a factor r. The decimated vector y is r times shorter in length than the input vector x. By default, decimate employs an eighth-order low pass Chebyshev Type I filter. It filters the input sequence in both the forward and reverse directions to remove all phase distortion, effectively doubling the filter order.

downsample: Decreases the sampling rate of the input signal

y = downsample(x,n) decreases the sampling rate of x by keeping every nth sample starting with the first sample. The downsampled y has length(x)/n samples

ALGORITHM/PROCEDURE:

1. Generate a sinusoidal waveform

2. Using the appropriate library function for interpolation ,decimation ,upsampling ,

downsampling and resampling, perform sampling rate conversion for the sinusoidal waveform

3. Find the spectrum of all the signals and compare them in frequency domain.

4. Display the resultant waveforms

Source code :clc;

clear all;

close all;

%continuous sinusoidal signal

a=input('Enter the amplitude:');

f=input('Enter the Timeperiod:');

t=-10:1:20;

x=a*sin(2*pi*f*t);

subplot(2,3,1);

plot(t,x);

xlabel('time');

ylabel('Amplitude');

title('Sinusoidal signal');

%decimating the signal

d=input('Enter the value by which the signal is to be decimated:');

y1=decimate(x,d);

subplot(2,3,2);

stem(y1);

xlabel('time');

ylabel('Amplitude');

title('Decimated signal');

%interpolating the signal

i=input('Enter the value by which the signal is to be interpolated:');

y2=interp(x,i);

subplot(2,3,3);

stem(y2);

xlabel('time');

ylabel('Amplitude');

title('Interpolated signal');

%resampling the signal

y3=resample(x,3,2);

subplot(2,3,4);

stem(y3);

xlabel('time');

ylabel('Amplitude');

title('Resampled signal');

%downsampling the signal

y4=downsample(x,2);

subplot(2,3,5);

stem(y4);

xlabel('time');

ylabel('Amplitude');

title('Downsampled signal');

%upsampling the signal

y5=upsample(x,3);

subplot(2,3,6);

stem(y5);

xlabel('time');

ylabel('Amplitude');

title('Upsampled signal');

Output :

RESULT:

The program written using library functions and the sampling rate conversion process is studied.FLOWCHART:

AIM:

Write a MATLAB Script to perform discrete convolution (Linear and Circular) for the given two sequences and also prove by manual calculation.

APPARATUS REQUIRED:

PC, MATLAB software

THEORY:

LINEAR CONVOLUTION:

The response y[n] of a LTI system for any arbitrary input x[n] is given by convolution of impulse response h[n] of the system and the arbitrary input x[n].

y[n] = x[n]*h[n] = or

If the input x[n] has N1 samples and impulse response h[n] has N2 samples then the output sequence y[n] will be a finite duration sequence consisting of (N1 + N2 - 1) samples. The convolution results in a non periodic sequence called Aperiodic convolution.

STEPS IN LINEAR CONVOLUTION:

The process of computing convolution between x[k] and h[k] involves four steps.

1. Folding: Fold h[k] about k=0 to obtain h[-k]

2. Shifting: Shift h[-k] by n0to right if n0 is positive and shift h[-k] by n0 to the left if n0 is negative. Obtain h[n0-k]

3. Multiplication : Multiply x[k] by h[n0-k] to obtain the product sequence

yn0 [k] = x[k] h [n0 k]

4. Summation: Find the sum of all the values of the product sequence to obtain values of output at n = n0Repeat steps 2 to 4 for all possible time shifts n0 in the range -l1 l3=l2-l1; x1=[x1,zeros(1,l3)];endf=cconv(x1,x2);disp('The Circular convoluted sequence is');disp(f);subplot(3,1,3);stem(f);xlabel('Time');ylabel('Amplitude');title('Circular Convoluted sequence');Command window :

OUTPUT :

SOURCE CODE :3clc;clear all;close all;%Program to perform Linear Convolution using Circular Convolution x1=input('Enter the first sequence to be convoluted:');subplot(3,1,1);l1=length(x1);stem(x1);xlabel('Time');ylabel('Amplitude');title('First sequence');x2=input('Enter the second sequence to be convoluted:');subplot(3,1,2);l2=length(x2);stem(x2);xlabel('Time');ylabel('Amplitude');title('Second sequence');if l1>l2 l3=l1-l2; x2=[x2,zeros(1,l3)];elseif l2>l1 l3=l2-l1; x1=[x1,zeros(1,l3)];endn=l1+l2-1;f=cconv(x1,x2,n);disp('The convoluted sequence is');disp(f);subplot(3,1,3);stem(f);xlabel('Time');ylabel('Amplitude');title('Convoluted sequence');Command window :

OUTPUT:

SOURCE CODE :4clc;clear all;close all;%Program to perform Linear Convolutionx=input('Enter the first input sequence:');l1=length(x);subplot(3,2,1);stem(x);xlabel('Time index n---->');ylabel('Amplitude');title('input sequence');h=input('Enter the system response sequence:');l2=length(h);subplot(3,2,2);stem(h);xlabel('Time index n---->');ylabel('Amplitude');title('system response sequence');if l1>l2 l3=l1-l2; h=[h,zeros(1,l3)];elseif l2>l1 l3=l2-l1; x=[x,zeros(1,l3)];endy=conv(x,h);disp('The time domain convoluted sequence is:');disp(y);subplot(3,2,3);stem(y);xlabel('Time index n---->');ylabel('Amplitude');title('convoluted output sequence');X=fft(x,length(y));H=fft(h,length(y));Y=X.*H;disp('The frequency domain multiplied sequence is:');disp(Y);subplot(3,2,4);stem(Y);xlabel('Time index n---->');ylabel('Amplitude');title('frequency domain multiplied response');y1=ifft(Y,length(Y));disp('The inverse fourier transformed sequence is:');disp(y1);subplot(3,2,5);stem(y1);xlabel('Time index n---->');ylabel('Amplitude');title('output after inverse fourier transform'); e=y-y1;disp('The Error sequence:')disp(abs(e));subplot(3,2,6);stem(abs(e));xlabel('Time index n---->');ylabel('Amplitude');title('error sequence');Command window :

OUTPUT :

SOURCE CODE :5clc;clear all;close all;%PROGRAM FOR CIRCULAR CONVOLUTION USING DISCRETE CONVOLUTION EXPRESSIONx=input('Enter the first sequence:');n1=length(x);h=input('Enter the second sequence:');n2=length(h);n=0:1:n1-1;subplot(3,1,1);stem(n,x);xlabel('Time');ylabel('Amplitude');title('First sequence Response x(n)');n=0:1:n2-1;subplot(3,1,2);stem(n,h);xlabel('Time');ylabel('Amplitude');title('Second sequence Response h(n)');n=n1+n2-1;if n1>n2 n3=n1-n2; h=[h,zeros(1,n3)];elseif n2>n1 n3=n2-n1; x=[x,zeros(1,n3)];endx=[x,zeros(1,n-n1)];h=[h,zeros(1,n-n2)];for n=1:n y(n)=0; for i=1:n j=n-i+1; if(j> that appears in the Command window.

3. Write the program in the Edit window and save it as m-file

4. Run the program

5. Enter the input in the command window

6. The result is displayed in the Command window and the graphical output is displayed in the Figure WindowSource Code :

clc;clear all;close all;%Get the sequence from user disp('The sequence from the user:');xn=input('Enter the input sequence x(n):');% To find the length of the sequenceN=length(xn);%To initilise an array of same size as that of input sequenceXk=zeros(1,N);iXk=zeros(1,N);%code block to find the DFT of the sequencefor k=0:N-1 for n=0:N-1 Xk(k+1)=Xk(k+1)+(xn(n+1)*exp((-i)*2*pi*k*n/N)); endend%code block to plot the input sequencet=0:N-1;subplot(3,2,1);stem(t,xn);ylabel ('Amplitude');xlabel ('Time Index');title ('Input Sequence');%code block to plot the X(k)disp('The discrete fourier transform of x(n):');disp(Xk);t=0:N-1;subplot(3,2,2);stem(t,Xk);ylabel ('Amplitude');xlabel ('Time Index');title ('X(k)');% To find the magnitudes of individual DFT pointsmagnitude=abs(Xk);%code block to plot the magnitude responsedisp('The magnitude response of X(k):');disp(magnitude);t=0:N-1;subplot(3,2,3);stem(t,magnitude);ylabel ('Amplitude');xlabel ('K');title ('Magnitude Response');%To find the phases of individual DFT pointsphase=angle(Xk);%code block to plot the phase responsedisp('The phase response of X(k):');disp(phase);t=0:N-1;subplot(3,2,4);stem(t,phase);ylabel ('Phase');xlabel ('K');title ('Phase Response');% Code block to find the IDFT of the sequencefor n=0:N-1 for k=0:N-1 iXk(n+1)=iXk(n+1)+(Xk(k+1)*exp(i*2*pi*k*n/N)); endendiXk=iXk./N;%code block to plot the output sequencet=0:N-1;subplot(3,2,5);stem(t,xn);ylabel ('Amplitude');xlabel ('Time Index');title ('IDFT sequence');%code block to plot the FFT of input sequence using inbuilt functionx2=fft(xn);subplot(3,2,6);stem(t,x2);ylabel ('Amplitude');xlabel ('Time Index');title ('FFT of input sequence');Command Window :

OUTPUT :

RESULT:

The program for DFT calculation was performed with library functions and without library functions. The results were verified by manual calculation.FLOWCHART:

AIM:

Write a MATLAB Script to compute Discrete Fourier Transform and Inverse Discrete Fourier Transform of the given sequence using FFT algorithms (DIT-FFT & DIF-FFT)

. APPARATUS REQUIRED:

PC, MATLAB software

THEORY:

DFT is a powerful tool for performing frequency analysis of discrete time signal and it is described as a frequency domain representation of a DT sequence.

The DFT of a finite duration sequence x[n] is given by

X (k) = k=0, 1.N-1

which may conveniently be written in the form

X (k) =

k=0, 1.N-1

where WN=e-j2(/N which is known as Twiddle or Phase factor.

COMPUTATION OF DFT:

To compute DFT, it requires N2 multiplication and (N-1) N complex addition. Direct computation of DFT is basically inefficient precisely because it does not exploit the symmetry and periodicity properties of phase factor WN.

FAST FOURIER TRANSFORM (FFT):

FFT is a method of having computationally efficient algorithms for the execution of DFT, under the approach of Divide and Conquer. The number of computations can be reduced in N point DFT for complex multiplications to N/2log2N and for complex addition to N/2log2N.

Types of FFT are,

(i) Decimation In Time (DIT)

(ii) Decimation In Frequency (DIF)

IDFT USING FFT ALGORITHMS:

The inverse DFT of an N point sequence X (k), where k=0,1,2N-1 is defined as ,

x [n] =

where, wN=e-j2(/N.

Taking conjugate and multiplying by N, we get,

N x*[n] =

The right hand side of the equation is the DFT of the sequence X*(k). Now x[n] can be found by taking the complex conjugate of the DFT and dividing by N to give,

x [n]=

RADIX-2 DECIMATION IN TIME FFT:

The idea is to successively split the N-point time domain sequence into smaller sub sequence. Initially the N-point sequence is split into xe[n] and xo[n], which have the even and odd indexed samples of x[n] respectively. The N/2 point DFTs of these two sequences are evaluated and combined to give N-point DFT. Similarly N/2 point sequences are represented as a combination of two N/4 point DFTs. This process is continued, until we are left with 2 point DFT.

RADIX-2 DECIMATION IN FREQUENCY FFT:

The output sequence X(k) is divided into smaller sequence.. Initially x[n] is divided into two sequences x1[n], x2[n] consisting of the first and second N/2 samples of x[n] respectively. Then we find the N/2 point sequences f[n] and g[n] as

f[n]= x1[n]+x2[n],

g[n]=( x1[n]-x2[n] )wNk

The N/2 point DFT of the 2 sequences gives even and odd numbered output samples. The above procedure can be used to express each N/2 point DFT as a combination of two N/4 point DFTs. This process is continued until we are left with 2 point DFT.

LIBRARY FUNCTION:

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-Dimensional arrays, the FFT operation operates on the first non-singleton dimension.

ditfft: Decimation in time (DIT)fft

ditfft(x) is the discrete Fourier transform (DFT) of vector x in time domain decimation

diffft: Decimation in frequency (DIF)fft

diffft(x) is the discrete Fourier transform (DFT) of vector x in Frequency domain decimation

ALGORITHM/PROCEDURE:

1. Input the given sequence x[n]

2. Compute the Discrete Fourier Transform using FFT library function (ditfft or diffft) and obtain X[k]

3. Compute the Inverse Discrete Fourier Transform using FFT library function (ditfft or diffft) and obtain X[n] by following steps

a. Take conjugate of X [k] and obtain X[k]*

b. Compute the Discrete Fourier Transform using FFT library function (ditfft or diffft) for X[k]* and obtain N.x[n]*

c. Once again take conjugate for N.x[n]* and divide by N to obtain x[n]

4. Display the results.

SOURCE CODE:(DITFFT)clc;clear all;close all;N=input('Enter the number of elements:');for i=1:N re(i)= input('Enter the real part of the element:'); im(i)= input('Enter the imaginary part of the element:');end%% Call Dit_fft function[re1,im1]= ditfft(re,im,N);disp(re1);disp(im1);figure(1);subplot(2,2,1);stem(re1);xlabel('Time period');ylabel('Amplitude');title('Real part of the output');subplot(2,2,2);stem(im1);xlabel('Time period');ylabel('Amplitude');title('Imaginary part of the output');%%dit_ifftN=input('Enter the number of elements:');for i=1:N re(i)= input('Enter the real part of the element:'); im(i)= input('Enter the imaginary part of the element:');endfor i=1:N re(i)=re(i); im(i)=-im(i);end%% call dit_ifft function[re1,im1]=ditifft(re,im,N);for i=1:N re1(i)=re1(i)/N; im1(i)=-im1(i)/N;enddisp(re1);disp(im1);%figure(2)subplot(2,2,3);stem(re1);xlabel('Time period');ylabel('Amplitude');title('Real part of the output');subplot(2,2,4);stem(im1);xlabel('Time period');ylabel('Amplitude');title('Imaginary part of the output');Function Table:(DITFFT)

function [ re, im ] = ditfft( re, im, N)%UNTITLED5 Summary of this function goes here% Detailed explanation goes hereN1=N-1;N2=N/2;j=N2+1;M=log2(N);% Bit reversal sortingfor i=2:N-2 if i that appears in the Command window.

3. Write the program in the Edit window and save it in M-file

4. Run the program

5. Enter the input in the command window

6. The result is displayed in the Command window and the graphical output is displayed in the Figure Window

SOURCE CODE:PROGRAM 1clc;clear all;close all;%% Input the samples% Time domain response of FIR filterN=16; %Input samplesk=0:N-1;x=(k==0);b0=1; b1= -1; b2=-2;B=[b0,b1,b2]; %Numerator coeff.A=1; %Denominator coeff.%Filteringy=filter(B, A ,x);%Plot the graphsubplot(2,2,1), stem(k,x,'r');xlabel('Time');ylabel('Unit Impulse');title('Impulse input');subplot(2,2,2), stem(k,y,'r');xlabel('Frequency');ylabel('Magnitude');title('Impulse Response FIR Filter');% Time domain Response of IIR FilterN1=10; %input samplesk1=0:N1-1;x1=(k1==0);B1=1;a=0.8;A1=[1,-a];y1=filter(B1, A1 ,x1);%plot the graphsubplot(2,2,3), stem(k1,x1,'r');xlabel('Time');ylabel('Unit Impulse');title('Impulse input');subplot(2,2,4), stem(k1,y1,'r');xlabel('Frequency');ylabel('Magnitude');title('Impulse Response IIR Filter');PROGRAM 2

clc;clear all;close all;%% Input the samples% Time domain Response of FIR filterN=16; %Input samplesk=0:N-1;x(1:N)=1;b0=1; b1= -1; b2=-2;B=[b0,b1,b2]; %Numerator coeff.A=1; %Denominator coeff.%Filteringy=filter(B, A ,x);%plot the graphsubplot(2,2,1);stem(k,x,'r');xlabel('Time');ylabel('Unit step signal');title('Unit step input signal');subplot(2,2,2);stem(k,y,'r');xlabel('Frequency');ylabel('Magnitude');title('Step Response FIR filter');% Time domain response of IIR filterN1=10; %Input samplesk1=0:N1-1;x1(1:N1)=1;B1=1;a=0.8;A1=[1,-a];y1=filter(B1, A1 ,x1);%plot the graphsubplot(2,2,3);stem(k1,x1,'r');xlabel('Time');ylabel('Unit step input');title('Unit step input signal');subplot(2,2,4);stem(k1,y1,'r');xlabel('Frequency');ylabel('Magnitude');title('Step Response IIR Filter');Output for Unit impulse input:

Output for Unit step input:

RESULT:The Time domain responses of the given systems were found using MATLAB and manually verified.FLOWCHART:

AIM:

Write a MATLAB Script to find the frequency domain response (magnitude response and phase response) for the given FIR and IIR systems (filters).

. APPARATUS REQUIRED:

PC, MATLAB software

THEORY:

IMPULSE RESPONSE:

x[n] y[n]=T[x[n]]

If the input to the system is a unit impulse (ie) x[n]=([n], then the output of the system is known as impulse response denoted by h[n] where,

h[n]=T[([n]]

We know that any arbitrary sequence x[n] can be represented as a weighted sum of discrete impulses. Now the system response is given by,

y[n]=T[x[n]]= T[]

where x(k) denotes the kth sample. The response of DTLTI system to sequence x(k) ([n-k] will be x(k)h[n-k].i.e. T[x(k) ([n-k]] = x(k) T[([n-k]] = x(k) h[n-k].So response y[n] of DTLTI system to x[n] is

y [n]=

This is known as convolution sum and can be represented as

y[n] = x[n]*h[n]

* is the Convolution operator

FREQUENCY RESPONSE:

y[n] can also be written as

(y[n]=

If x[n] is a complex exponential of the form x[n] =ej(n where n varies from - to

Then y[n] =

=

=

y[n]=H(ej() ej(n

where H(ej()=

H(ej() is called the frequency response of DTLTI system. It is a complex function

H(ej() =Hr(ej( )+j Him(ej()

H(ej()=ej((()

is the magnitude response. is the phase response.

LIBRARY FUNCTIONS:

exp: Exponential.

exp (X) is the exponential of the elements of X, e to the power X. For complex Z=X+i*Y, exp (Z) = exp (X)*(COS(Y) +i*SIN(Y)).

disp: Display array.

DISP (X) is called for the object X when the semicolon is not used to terminate a statement.

Freqz: Compute the frequency response of discrete-time filters, [h,w] = freqz (hd) returns the frequency response vector h and the corresponding frequency vector w for the discrete-time filter hd. When hd is a vector of discrete-time filters, freqz returns the matrix h. Each column of h corresponds to one filter in the vector hd.

Angle: Phase angle

P = angle (Z) returns the phase angle, in radians, for each element of complex array Z.

log10:

The log10 function operates element-by-element on arrays. Its domain includes complex numbers, which may lead to unexpected results if used unintentionally ALGORITHM/PROCEDURE:

1. Click on the MATLAB icon on the desktop (or go to Start All programs and click on MATLAB) to get into the Command Window

2. Type edit in the MATLAB prompt >> that appears in the Command window.

3. Write the program in the Edit window and save it in M-file.4. Run the program.5. Enter the input in the command window.6. The result is displayed in the Command window and the graphical output is displayed in the Figure Window.

SOURCE CODE:

clc;clear all;close all;%Frequency Responsenum=input('Enter num:');denum=input('Enter denum:');n=linspace(0,pi,1000);h=freqz(num,denum,n);mag=20*log(abs(h));subplot(2,2,1),semilogx(n,mag);xlabel('Frequency index'),ylabel('Magnitude'),title('Magnitude Response');pha=angle(h);subplot(2,2,2),semilogx(n,pha);xlabel('Frequency index'),ylabel('Phase'),title('Phase Response');z=tf(num,denum,1)COMMAND WINDOW:

OUTPUT :

RESULT:

The frequency response (magnitude and phase response) of the given system was found using MATLAB.

START

ENTER THE SIGNAL PARAMETERS (AMPLITUDE, TIME AND FREQUENCY)

Generate the waveform by using the appropriate library function

PLOT THE WAVEFORMS

STOP

Ex. No:1

Date:09-12-13

WAVEFORM GENERATION

x

t

t

t

(

)

(

)

,

1

0

0

for

,

otherwise

x

u

t

t

(t

)

(

)

,

1

0

0

for

,

otherwise

x

t

r

t

t

t

(

)

(

)

,

for

,

otherwise

0

0

x

t

A

(

)

sin(

)

t

x

t

at

(

)

= A e at

START

READ THE INPUT SEQUENCE

PLOT THE WAVEFORMS

STOP

READ THE CONSANT FOR (SCALAR) AMPLITUDE AND TIME MANIPULATION

READ THE (VECTOR) SEQUENCE FOR SIGNAL ADDTION AND MULTIPLICATION

PERFORM OPERTAION ON THE D.T. SIGNAL

Ex. No:2

Date :21-12-13

BASIC OPERATIONS ON D.T SIGNALS

START

READ THE INPUT SEQUENCE

PLOT THE WAVEFORMS

STOP

READ THE CONSANT FOR (SCALAR) AMPLITUDE AND TIME MANIPULATION

READ THE (VECTOR) SEQUENCE FOR SIGNAL ADDTION AND MULTIPLICATION

PERFORM OPERTAION ON THE D.T. SIGNAL using the appropriate library function

Ex. No: 3

Date: 06-1-14

PROPERTIES OF DISCRETE TIME SYSTEM

START

ENTER THE SIGNAL PARAMETERS (AMPLITUDE, TIME AND FREQUENCY)

FIND THE SPectrum of

all the signals

PLOT THE WAVEFORMS

STOP

PERFORM THE SAMPLING RATE CONVERSION ON THE INPUT BY using upsample, DOWNSAMPLE and resample

PERFORM interpolation and decimation ON THE INPUT

Ex. No: 4

Date: 13-1-14

SAMPLING RATE CONVERSION

ANTIALIASING

FILTER H (Z)

(M

(L

ANTI IMAGING FILTER H (Z)

UPSAMPLER (

ANTI IMAGING FILTER

ANTI ALIASING FILTER

DOWN SAMPLER (

START

ENTER THE INPUT SEQUENCE x[n] & SYSTEM RESPONSE h[n]

PERFORM LINEAR AND CIRCULAR CONVOLUTION IN TIME DOMAIN

PLOT THE WAVEFORMS AND ERROR

STOP

Ex. No: 5

Date: 20-1-14

DISCRETE CONVOLUTION

START

ENTER THE INPUT SEQUENCE

PERFORM THE DFT USING IN-builT FFT AND USING DIRECT FORMULA ON THE GIVEN INPUT SEQUENCE

PLOT THE WAVEFORMS AND ERROR

STOP

Ex. No: 6

Date : 27-1-14

DISCRETE FOURIER TRANSFORM

START

ENTER THE INPUT SEQUENCE IN TIME DOMAIN OR FREQUENCY DOMAIN

PERFORM DIT/DIF-FFT FOR TIME SAMPLES or PERFORM IDIT/IDIT-FFT FOR FREQuency SAMPLES

PLOT THE WAVEFORMS

STOP

Ex. No:7

Date:3-2-14

FAST FOURIER TRANSFORM ALGORITHMS

START

ENTER THE FILTER SPECIFICATIONS (ORDER OF THE FILTER, CUT-OFF FREQUENCY)

dESIGN THE FILTER

PLOT THE WAVEFORMS

STOP

Ex. No:8

Date:10-2-14

DESIGN OF FIR FILTERS

START

ENTER THE FILTER SPECIFICATIONS (PASS, STOP BAND GAINS AND EDGE FREQUENCIES)

DESIGN THE ANALOG BUTTERWORTH

and chebyshev FILTER

PLOT THE WAVEFORMS

STOP

Ex. No: 9

Date: 17-02-14

9.DESIGN OF BUTTERWORTH FILTERS

START

ENTER THE FILTER SPECIFICATIONS (PASS, STOP BAND GAINS AND EDGE FREQUENCIES)

DESIGN THE ANALOG BUTTERWORTH

and chebyshev FILTER

PLOT THE WAVEFORMS

STOP

Ex. No: 10

Date: 17-02-14

10.DESIGN OF CHEBYSHEV FILTERS

START

ENTER THE FILTER SPECIFICATIONS (PASS, STOP BAND GAINS AND EDGE FREQUENCIES)

DESIGN THE ANALOG BUTTERWORTH and CHEBYSHEV LOW PASS FILTER

PLOT THE WAVEFORMS

STOP

CONVERT THE LOW PASS FILTERS in to digital filterS by using the impulse invariant and bilinear TRANSFORMATIONS

Ex. No: 11

Date: 03-03-14

11.DESIGN OF IIR FILTERS

START

ENTER THE SYSTEMS NUMERATOR AND DENOMINATOR COEFFICIENTS

Generate the waveform of TIME DOMAIN response by using the appropriate library function

PLOT THE WAVEFORMS

STOP

Ex. No:12(a)

Date:10-03-14

TIME DOMAIN RESPONSE OF LTI SYSTEMS

T

T

START

ENTER THE SYSTEMS NUMERATOR AND DENOMINATOR COEFFICIENTS

Generate the waveform of frequencY

PLOT THE WAVEFORMS

STOP

Ex. No: 12(b)

Date:10-03-14

FREQUENCY DOMAIN RESPONSE OF LTI SYSTEMS

T

T

27

_1185707736.unknown

_1324750162.unknown

_1324782052.unknown

_1326216744.unknown

_1326224232.unknown

_1326224347.unknown

_1326224178.unknown

_1324782393.unknown

_1324782450.unknown

_1324782326.unknown

_1324781483.unknown

_1324781660.unknown

_1324750235.unknown

_1187524556.unknown

_1187524891.unknown

_1324746662.unknown

_1324746705.unknown

_1187525085.unknown

_1187525543.unknown

_1187525616.unknown

_1187525445.unknown

_1187524959.unknown

_1187524758.unknown

_1187524791.unknown

_1187524730.unknown

_1187522534.unknown

_1187522617.unknown

_1187166941.unknown

_1183897482.unknown

_1185063004.unknown

_1185108735.unknown

_1185108802.unknown

_1185063018.unknown

_1185059633.unknown

_1183288882.unknown

_1183897438.unknown

_1183288779.unknown