MATLAB Practical Copy

Name Roll No. Class : : : Gaurish Kumar Tripathi 09 M-Tech, 1st Semester J. K. Institute of Applied Physics and Technology University of Allahabad

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

05. >>x=sin(2*pi*100*t).01 Second For taking 100 uniform intervals in one cycle. >>title('Sinewave'). >>t = 0:ts:0.05. Let us consider the sinusoidal signal has fixed frequency (f) of 100 Hz. . >>ts = 1/fs.1). >>plot(t. Time period = 1 Second Since we have to take separation between two points = 0.x). the sampling time = = 0.01.1. >>subplot(2. >>grid Program (Object 2): >>%Sampling time = 0. Time period = 0. >>xlabel('Time'). Take separation between two points 0. Program (Object 1): >> fs = 10000. Let us consider the sinusoidal signal has fixed frequency (f) of 1Hz. >>ylabel('Amplitude').01 Object: (i) (ii) Theory: Sine is an analog signal that is used to represent behavior of various phenomenons in time domain. Take 100 uniform intervals in one cycle. Generate and plot sine wave consisting of one frequency only.05 sec >>%Sampling frequency = 20Hz Generate and plot sine wave consisting of one frequency only.Experiment Number .0001 second Thus sampling frequency = 10000 Hz. thus sampling frequency should be equal to 20 Hz.

>>subplot(2. >>ylabel('Amplitude').smpling time = 0.1. >>ts = 1/fs. >>xlabel('Time'). .1). >>t = 0:ts:1.x). >>x=sin(2*pi*1*t). >>plot(t. >>title('Sinewave. >>grid Results: The plotted sine waves are enclosed in figure 1 and figure 2.05s').frequency = 1Hz.>>%Let sine wave frequency = 1Hz >>fs = 20.

Figure: 1 Figure: 2 .

Experiment Number . n = [nmin:nmax]. nh = [-3:3]. >>y = conv(x.h) Where x and h are finite sequences written in vector form Program: >>x = [1 2 2 1 2]. '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.y. 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 Syntax: >>y=conv(x. This function assumes that the two sequences have been defined as vectors and it generates an output sequence that is also a vector.02 Object: Write a program for linear convolution Theory: The convolution sum of two sequences x[n] and h[n] is defined as  y  MATLAB has a built in function conv to perform convolution of finite length sequences of values. >>nmin = min(nx) + min(nh). >>nmax = max(nx) + max(nh). . is equal to the extent of x[n] plus the h[n].h). nx = [-2:2]. >>h = [2 2 -1 1 2 2 1]. >>stem(n.

Figure: 1 Convolution of two sequences .

maxlags) returns the autocorrelation sequence over the lag range [maxlags:maxlags]. c is a matrix with 2*maxlags+1 rows whose P2 columns contain the autocorrelation sequences for all combinations of the columns of x. Output c has length 2*maxlags+1. the shorter vector is zeropadded to the length of the longer vector. 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.y) returns the cross-correlation sequence in a length 2*N-1 vector. c = xcorr(x. where x and y are length N vectors (N>1).4] >>%initial condition [1000000] >>%length = 2^N-1 = 127 >>% initial condition >>k=1. >>length = 127.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) is the autocorrelation sequence for the vector x. Syntax: >>y= xcorr(x) Program: >> %Program for sequence generation >>%7 bit feedback register. If x is an N-by-P matrix.Experiment Number . If x and y are not the same length. . feedback from [7. If x is an N-by-P matrix.

lag). >>end >>%change s7 to +1 and -1 volt >>seq=2*s7-1. >>for k=2:length >>s1(k) = xor(s4(k-1). >>n=1:length. >>s5(k) = 0. >>s2(k) = s1(k-1). >>s7(k) = s6(k-1). >>s3(k) = 0. >>s4(k) = s3(k-1). . >>lag=2^m+2. >>l=-lag:lag.>>s1(k) = 1. >>s5(k) = s4(k-1). >>s6(k) = s5(k-1). >>s2(k) = 0. %time shift limit >>c=xcorr(seq. >>plot(l.c) Results: The maximal length sequence and its autocorrelation are plotted in figure 1 and figure2. >>stairs(n. >>s4(k) = 0.seq) >>figure >>m=7. >>s7(k) = 0.s7(k-1)). >>s3(k) = s2(k-1). >>s6(k) = 0.

Figure: 1 Maximal length sequence Figure: 1 Autocorrelation .

Here I have selected the sampling frequency of 1000Hz.1). Plot their FFT Add AWGN noise and determine the spectrum of corrupted signal .04 Object: (i) (ii) Theory: The signal is generated with frequency of 40 Hz and 150Hz. the FFT operation is applied to each column.N) is the N-point FFT. Program (Object 1): >>fs = 1000. >>f = fs*(-255:256)/512. padded with zeros if X has less than N points and truncated if it has more. The sampling frequency was selected more than highest signal frequency.x). Generate signal (Combinations of sine waves).512). >>mag1 = fftshift(abs(spec1)).Experiment Number . >>x = cos(2*pi*40*t) + cos(2*pi*150*t). the FFT operation operates on the first non-singleton dimension. >>plot(t. For N-D arrays.1. >>t = 0:ts:1. >>subplot(2. 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. FFT(X. >>title('Signal without noise') >>grid >>spec1 = fft(x. >>ts = 1/fs.

>>title('signal corrupted with noise') >>grid >>spec2 = fft(y.mag1).y). >>title('FFT plot of signal without noise') >>grid Program (Object 2): >>fs = 1000. >>plot(f. >>title('FFT plot of signal with noise') >>grid Results: (i) (ii) Signal generated and their FFT magnitude spectrum is shown in figure 1. >>y = x + noise. >>t = 0:ts:1. >>subplot(2.1). .512). >>f = fs*(-255:256)/512.>>subplot(2. >>plot(f.mag2). >>noise = randn(size(x)). >>plot(t. >>ts = 1/fs. >>x = cos(2*pi*40*t) + cos(2*pi*150*t). >>subplot(2.1. >>mag2 = fftshift(abs(spec2)).2).2). The corrupted signal by AWGN with their spectrum is shown in figure 2.1.1.

Figure 1: Signal with their spectrum Figure 2: Corrupted signal and their spectrum .

Experiment Number . µfilled¶). %specify index n %define the delta sequence n0 n=0 >>stem(n. >>delta = (n==0). %Plot the delta sequence Program (Object 2): >> n = -30:30. delta. %specify index n %define the step sequence %Plot the step sequence Results: (i) (ii) Delta sequence is plotted at figure 1. >>step = (n>=0). >>stem(n.05 Object: Generate and plot (i) Unit Step (ii) Unit Impulse Theory: The unit step sequence is defined as u[n] 1 0 n•0 n<0 The impulse or delta sequence plays an important role in characterization of discrete-time linear time invariant system. Step sequence is plotted at figure 2. step. . µfilled¶). The impulse sequence written as and defined as 1 = 0 Program (Object 1): >>n = -30:30.

Figure 1: Delta sequence Figure 2: Step sequence .