You are on page 1of 16

MATLAB Practical Copy

Name : Gaurish Kumar Tripathi

Roll No. : 09

Class : M-Tech, 1st Semester

J. K. Institute of Applied Physics and Technology

University of Allahabad
Index
Sl. Experiment Name Date Page No. Teacher’s signature
No.
01 (i) Generate and plot sine wave
consisting of one frequency
only. Take 100 uniform
intervals in one cycle.
(ii) Generate and plot sine wave
consisting of one frequency
only. Take separation between
two points 0.05.
02 (i) Write a program for linear
convolution
03 (i) Write a program for
Autocorrelation
04 (i) Generate signals (Combination
of sine waves), add noise to
the generated signal. Plot it.
(ii) Plot the FFT of signal with and
without noise.

05 Generate and plot


(i) Unit Step Sequence
(ii) Unit impulse
06
Experiment Number - 01
Object:

(i) Generate and plot sine wave consisting of one frequency only. Take 100
uniform intervals in one cycle.
(ii) Generate and plot sine wave consisting of one frequency only. Take
separation between two points 0.05.

Theory:

Sine is an analog signal that is used to represent behavior of various phenomenons in


time domain.
Let us consider the sinusoidal signal has fixed frequency (f) of 100 Hz.
Time period = 0.01 Second
0.01
For taking 100 uniform intervals in one cycle, the sampling time = ¿
100
= 0.0001 second
Thus sampling frequency = 10000 Hz.

Let us consider the sinusoidal signal has fixed frequency (f) of 1Hz.
Time period = 1 Second
Since we have to take separation between two points = 0.05, thus sampling frequency
should be equal to 20 Hz.

Program (Object 1):

>> fs = 10000;
>>ts = 1/fs;
>>t = 0:ts:0.01;
>>x=sin(2*pi*100*t);
>>subplot(2,1,1);
>>plot(t,x);
>>xlabel('Time');
>>ylabel('Amplitude');
>>title('Sinewave');
>>grid

Program (Object 2):


>>%Sampling time = 0.05 sec
>>%Sampling frequency = 20Hz
>>%Let sine wave frequency = 1Hz
>>fs = 20;
>>ts = 1/fs;
>>t = 0:ts:1;
>>x=sin(2*pi*1*t);
>>subplot(2,1,1);
>>plot(t,x);
>>xlabel('Time');
>>ylabel('Amplitude');
>>title('Sinewave,frequency = 1Hz,smpling time = 0.05s');
>>grid
Results:

The plotted sine waves are enclosed in figure 1 and figure 2.


Figure: 1

Figure: 2
Experiment Number - 02
Object:

Write a program for linear convolution

Theory:

The convolution sum of two sequences x[n] and h[n] is defined as


y [ n ] =x [ n ]∗h[n]

k=∞
y[ n ] = ∑ x [ k ] h [n−k ]
k=−∞

MATLAB has a built in function conv to perform convolution of finite length sequences of
values. This function assumes that the two sequences have been defined as vectors and it
generates an output sequence that is also a vector. Convolving a sequence x[n] of length
N with sequence h[n] with length M results in a sequence of length L = M+N+1. The extent
of x [ n ]∗h[n] is equal to the extent of x[n] plus the h[n].
Syntax:
>>y=conv(x,h)
Where x and h are finite sequences written in vector form
Program:
>>x = [1 2 2 1 2]; nx = [-2:2];
>>h = [2 2 -1 1 2 2 1]; nh = [-3:3];
>>nmin = min(nx) + min(nh);
>>nmax = max(nx) + max(nh);
>>y = conv(x,h); n = [nmin:nmax];
>>stem(n,y, 'filled')
>>title('Convolution of two sequence')
>>xlabel('Index[n]')
>>ylabel('y[n]=x[n]*h[n]')
>>grid
Results:
The Convolution of two sequences is plotted in figure 1.

Figure: 1 Convolution of two sequences


Experiment Number - 03
Object:

Write a program for Autocorrelation

Theory:

The autocorrelation of a sequence is defined as cross correlation of a sequence with itself.


c = xcorr(x,y) returns the cross-correlation sequence in a length 2*N-1 vector, where x and
y are length N vectors (N>1). If x and y are not the same length, the shorter vector is zero-
padded to the length of the longer vector.

c = xcorr(x) is the autocorrelation sequence for the vector x. If x is an N-by-P matrix, c is a


matrix with 2N-1 rows whose P2 columns contain the cross-correlation sequences for all
combinations of the columns of x.

c = xcorr(x,maxlags) returns the autocorrelation sequence over the lag range [-


maxlags:maxlags]. Output c has length 2*maxlags+1. If x is an N-by-P matrix, c is a matrix
with 2*maxlags+1 rows whose P2 columns contain the autocorrelation sequences for all
combinations of the columns of x.

Syntax:
>>y= xcorr(x)

Program:
>> %Program for sequence generation
>>%7 bit feedback register, feedback from [7,4]
>>%initial condition [1000000]
>>%length = 2^N-1 = 127
>>% initial condition
>>k=1;
>>length = 127;
>>s1(k) = 1;
>>s2(k) = 0;
>>s3(k) = 0;
>>s4(k) = 0;
>>s5(k) = 0;
>>s6(k) = 0;
>>s7(k) = 0;
>>for k=2:length
>>s1(k) = xor(s4(k-1),s7(k-1));
>>s2(k) = s1(k-1);
>>s3(k) = s2(k-1);
>>s4(k) = s3(k-1);
>>s5(k) = s4(k-1);
>>s6(k) = s5(k-1);
>>s7(k) = s6(k-1);
>>end
>>%change s7 to +1 and -1 volt
>>seq=2*s7-1;
>>n=1:length;
>>stairs(n,seq)
>>figure
>>m=7;
>>lag=2^m+2; %time shift limit
>>c=xcorr(seq,lag);
>>l=-lag:lag;
>>plot(l,c)
Results:

The maximal length sequence and its autocorrelation are plotted in figure 1 and
figure2.
Figure: 1 Maximal length sequence

Figure: 1 Autocorrelation
Experiment Number - 04
Object:
(i) Generate signal (Combinations of sine waves). Plot their FFT
(ii) Add AWGN noise and determine the spectrum of corrupted signal

Theory:

The signal is generated with frequency of 40 Hz and 150Hz. The sampling frequency was
selected more than highest signal frequency. Here I have selected the sampling frequency
of 1000Hz.

The signal was generated as x = cos(2*pi*f1*t) + cos(2*pi*f2*t)

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.

Program (Object 1):


>>fs = 1000;
>>ts = 1/fs;
>>t = 0:ts:1;
>>x = cos(2*pi*40*t) + cos(2*pi*150*t);
>>subplot(2,1,1);
>>plot(t,x);
>>title('Signal without noise')
>>grid
>>spec1 = fft(x,512);
>>mag1 = fftshift(abs(spec1));
>>f = fs*(-255:256)/512;
>>subplot(2,1,2);
>>plot(f,mag1);
>>title('FFT plot of signal without noise')
>>grid
Program (Object 2):
>>fs = 1000;
>>ts = 1/fs;
>>t = 0:ts:1;
>>x = cos(2*pi*40*t) + cos(2*pi*150*t);
>>noise = randn(size(x));
>>y = x + noise;
>>subplot(2,1,1);
>>plot(t,y);
>>title('signal corrupted with noise')
>>grid
>>spec2 = fft(y,512);
>>mag2 = fftshift(abs(spec2));
>>f = fs*(-255:256)/512;
>>subplot(2,1,2);
>>plot(f,mag2);
>>title('FFT plot of signal with noise')
>>grid

Results:

(i) Signal generated and their FFT magnitude spectrum is shown in figure 1.

(ii) The corrupted signal by AWGN with their spectrum is shown in figure 2.
Figure 1: Signal with their spectrum

Figure 2: Corrupted signal and their spectrum


Experiment Number - 05
Object:
Generate and plot
(i) Unit Step
(ii) Unit Impulse

Theory:

The unit step sequence is defined as u[n]


1 n≥0
u [ n ] =¿
1 n<0
The impulse or delta sequence plays an important role in characterization of discrete-time
linear time invariant system. The impulse sequence written as δ [n] and defined as

1 n=0
δ [n] =
0 n≠0

Program (Object 1):


>>n = -30:30; %specify index n
>>delta = (n==0); %define the delta sequence
>>stem(n, delta, ‘filled’); %Plot the delta sequence

Program (Object 2):


>> n = -30:30; %specify index n
>>step = (n>=0); %define the step sequence
>>stem(n, step, ‘filled’); %Plot the step sequence

Results:

(i) Delta sequence is plotted at figure 1.

(ii) Step sequence is plotted at figure 2.


Figure 1: Delta sequence

Figure 2: Step sequence

You might also like