EXPERIMENT-1 Aim-Introduction to MATLAB Theory- Introduction to MATLAB

21
Index S. No. Experiment Date Signatur e

Transcript of EXPERIMENT-1 Aim-Introduction to MATLAB Theory- Introduction to MATLAB

IndexS.No.

Experiment Date Signature

EXPERIMENT- 1

Aim- Introduction to MATLABTheory-Introduction to MATLAB

Matlab is an interpreted language for numerical computation. Itallows one to perform numerical calculations, and visualize theresults without the need for complicated and time consumingprogramming. Matlab allows its users to accurately solveproblems, produce graphics easily and produce code effeciently.

MATLAB programs are stored as plain text in files having namesthat end with the extension ``.m''. These files are called m-files. MATLAB functions have two parameter lists, one for inputand one for output. One nifty difference between MATLAB andtraditional high level languages is that MATLAB functions can beused interactively. In addition to providing the obvious supportfor interactive calculation, it also is a very convenient way todebug functions that are part of a bigger project.

Windows-

Command window- The window where we type commands and non-graphic output is displayed. A ‘>>’ prompt shows the system isready for input. The lower left hand corner of the main windowalso displays ‘Ready’ or ‘Busy’ when the system is waiting orcalculating. Previous commands can be accessed using the up arrowto save typing and reduce errors. Typing a few charactersrestricts this function to commands beginning with thosecharacters.

Figure window- MATLAB directs graphics output to a window that isseparate from the Command Window. In MATLAB, this window isreferred to as a figure. Graphics functions automatically create

new figure windows if none currently exist. If a figure windowalready exists, MATLAB uses that window. If multiple figurewindows exist, one is designated as the current figure and isused by MATLAB.

Editor window- The window where we edit m-files — the filesthat hold scripts and functions that we’ve defined or areediting. Multiple files are generally opened as tabs in the sameeditor window, but they can also be tiled for side by sidecomparison. Orange warnings and red errors appear as underliningand as bars in the margin. Hovering over them provides moreinformation; clicking on the bar takes we to the relevant bitof text. Also MATLAB runs the last saved version of a file, so wehave to save before any changes take effect.

Commands-Clc- clears command windowClear all- Deletes all variables from current workspaceClose all- Closes all open figure windowsFor (loop)- Iterates over procedure incrementing i by 1Subplot- Divide the plot window up into piecesStem- Plot discrete sequence dataPlot-

plot(x,y)- plot the elements of vector y (on the verticalaxis of a figure) versus the elements of the vector x (onthe horizontal axis of the figure)

plot(w,abs(y))- plot the magnitude of vector y (on thevertical axis of a figure) versus the elements of the vectorw (on the horizontal axis of the figure)

plot(w,angle(y))- plot the phase of vector y in radian (onthe vertical axis of a figure) versus the elements of thevector w (on the horizontal axis of the figure)

Label- Xlabel- Add a label to the horizontal axis of the current

plot Ylabel- Add a label to the vertical axis of the current plot

Title- Add a title to the current plot

Conv- Convolution and polynomial multiplicationFigure- Create a new figure or redefine the current figure

EXPERIMENT- 2

Aim- To generate pseudo random noise signal using Matlab.

Software used- MATLAB

Theory-In cryptography, pseudo random noise (PRN) is a signal similar tonoise which satisfies one or more of the standard tests forstatistical randomness.

Although it seems to lack any definite pattern, pseudo randomnoise consists of a deterministic sequence of pulses that willrepeat itself after its period.

In cryptographic devices, the pseudo random noise pattern isdetermined by a key and the repetition period can be very long,even millions of digits.

Pseudo random noise is used in some electronic musicalinstruments, either by itself or as an input to subtractivesynthesis, and in many white noise machines.

In spread-spectrum systems, the receiver correlates a locallygenerated signal with the received signal. Such spread-spectrumsystems require a set of one or more "codes" or "sequences" suchthat

Like random noise, the local sequence has a very lowcorrelation with any other sequence in the set, or with thesame sequence at a significantly different time offset, orwith narrow band interference, or with thermal noise.

Unlike random noise, it must be easy to generate exactly thesame sequence at both the transmitter and the receiver, so

the receiver's locally generated sequence has a very highcorrelation with the transmitted sequence.

Program-clc;clear all;close all;N=200; % Define Number of samplesR1=2*(round(rand(1,N))-0.5); % Generate Normal Random Numbersplot(R1); % Plot of R1title('Random Signal');xlabel('Sample Number ---------->');ylabel('Amplitude ---------->');axis([0 N -1.5 1.5]);

Output-

0 20 40 60 80 100 120 140 160 180 200-1.5

-1

-0.5

0

0.5

1

1.5Random Signal

Sam ple Num ber ---------->

Amplitude ---------->

EXPERIMENT- 3

Aim- To find probability density function of a random variable.

Software used- MATLAB

Theory-In probability theory, a probability density function (pdf), ordensity of a continuous random variable, is a function thatdescribes the relative likelihood for this random variable totake on a given value. The probability of the random variablefalling within a particular range of values is given by theintegral of this variable’s density over that range—that is, itis given by the area under the density function but above thehorizontal axis and between the lowest and greatest values of therange. The probability density function is nonnegativeeverywhere, and its integral over the entire space is equal toone.

The terms "probability distribution function" and "probabilityfunction" have also sometimes been used to denote the probabilitydensity function. However, this use is not standard amongprobabilists and statisticians. In other sources, "probabilitydistribution function" may be used when the probabilitydistribution is defined as a function over general sets ofvalues, or it may refer to the cumulative distribution function,or it may be a probability mass function rather than the density.Further confusion of terminology exists because density functionhas also been used for what is here called the "probability massfunction".

Program-

clc;close all;clear all;u=1;sigma=3;x = -10:0.1:10;p = exp(-(x-u).*(x-u)/(2*sigma^2))/(sqrt(2*pi)*sigma);plot(p);title('PDF Curve');xlabel('X ---------->');ylabel('P(x) ---------->');

Output-

0 50 100 150 200 2500

0.02

0.04

0.06

0.08

0.1

0.12

0.14PDF Curve

X ---------->

P(x) ---------->

EXPERIMENT- 4

Aim- To find fast fourier transform of a given sequence.

Software used- MATLAB

Theory-

A fast Fourier transform (FFT) is an algorithm to compute thediscrete Fourier transform (DFT) and its inverse. A Fouriertransform converts time (or space) to frequency and vice versa;an FFT rapidly computes such transformations. As a result, fastFourier transforms are widely used for many applications inengineering, science, and mathematics. The basic ideas werepopularized in 1965, but some FFTs had been previously known asearly as 1805. Fast Fourier transforms have been described as"the most important numerical algorithm[s] of our lifetime".[1]

There are many different FFT algorithms involving a wide range ofmathematics, from simple complex-number arithmetic to grouptheory and number theory; this article gives an overview of theavailable techniques and some of their general properties, whilethe specific algorithms are described in subsidiary articleslinked below.

The DFT is obtained by decomposing a sequence of values intocomponents of different frequencies. This operation is useful inmany fields (see discrete Fourier transform for properties andapplications of the transform) but computing it directly from thedefinition is often too slow to be practical. An FFT is a way tocompute the same result more quickly: computing the DFT of Npoints in the naive way, using the definition, takes O(N2)arithmetical operations, while a FFT can compute the same DFT inonly O(N log N) operations. The difference in speed can beenormous, especially for long data sets where N may be in the

thousands or millions. In practice, the computation time can bereduced by several orders of magnitude in such cases, and theimprovement is roughly proportional to N / log(N). This hugeimprovement made the calculation of the DFT practical; FFTs areof great importance to a wide variety of applications, fromdigital signal processing and solving partial differentialequations to algorithms for quick multiplication of largeintegers.

Program-clc;clear all;close all;x = [1 3 8 4 5 6 7]N = length(x);for k = 1:Ny(k) = 0;for n = 1:Ny(k) = y(k)+x(n)*exp(-1i*2*pi*(k-1)*(n-1)/N)endendsubplot(2,1,1);stem(x);ylabel('Amplitude ---->');xlabel('n ---->');title('Input Sequence');subplot(2,1,2);stem(y);ylabel('Amplitude ---->');xlabel('K ---->');title('FFT Sequence');

Output-

1 2 3 4 5 6 70

2

4

6

8

Amplitude ---->

n ---->

Input Sequence

1 2 3 4 5 6 7-5

0

5

Amplitude ---->

K ---->

FFT Sequence

EXPERIMENT- 5

Aim- To find inverse fast fourier transform of a given sequence.

Software used- MATLAB

Theory-

A fast Fourier transform (FFT) is an algorithm to compute thediscrete Fourier transform (DFT) and its inverse. A Fouriertransform converts time (or space) to frequency and vice versa;an FFT rapidly computes such transformations. As a result, fastFourier transforms are widely used for many applications inengineering, science, and mathematics. The basic ideas werepopularized in 1965, but some FFTs had been previously known asearly as 1805. Fast Fourier transforms have been described as"the most important numerical algorithm[s] of our lifetime".[1]

There are many different FFT algorithms involving a wide range ofmathematics, from simple complex-number arithmetic to grouptheory and number theory; this article gives an overview of theavailable techniques and some of their general properties, whilethe specific algorithms are described in subsidiary articleslinked below.

The DFT is obtained by decomposing a sequence of values intocomponents of different frequencies. This operation is useful inmany fields (see discrete Fourier transform for properties andapplications of the transform) but computing it directly from thedefinition is often too slow to be practical. An FFT is a way tocompute the same result more quickly: computing the DFT of Npoints in the naive way, using the definition, takes O(N2)arithmetical operations, while a FFT can compute the same DFT inonly O(N log N) operations. The difference in speed can beenormous, especially for long data sets where N may be in thethousands or millions. In practice, the computation time can bereduced by several orders of magnitude in such cases, and theimprovement is roughly proportional to N / log(N). This hugeimprovement made the calculation of the DFT practical; FFTs areof great importance to a wide variety of applications, fromdigital signal processing and solving partial differentialequations to algorithms for quick multiplication of largeintegers.

Program-

clc;clear all;close all;x = [1 2 5 4 3 2 4]N = length(x);for n = 1:Ny(n) = 0;for k = 1:Ny(n) = y(n)+(1/N)*x(k)*exp(1i*2*pi*(k-1)*(n-1)/N);endendsubplot(2,1,1);stem(x);ylabel('Amplitude ---->');xlabel('K ---->');title('Input Sequence');subplot(2,1,2);stem(y);ylabel('Amplitude ---->');xlabel('n ---->');title('IFFT Sequence');

Output-

1 2 3 4 5 6 70

2

4

6Am

plitude ---->

K ---->

Input Sequence

1 2 3 4 5 6 7-1

-0.5

0

0.5

1

Amplitude ---->

n ---->

IFFT Sequence

EXPERIMENT- 6

Aim- To perform auto- correlation of a given sequence.

Software used- MATLAB

Theory-

Autocorrelation is the cross-correlation of a signal with itself.Informally, it is the similarity between observations as afunction of the time lag between them. It is a mathematical toolfor finding repeating patterns, such as the presence of aperiodic signal obscured by noise, or identifying the missingfundamental frequency in a signal implied by its harmonicfrequencies. It is often used in signal processing for analyzingfunctions or series of values, such as time domain signals.

One application of autocorrelation is the measurement ofoptical spectra and the measurement of very-short-duration light pulses produced by lasers, both usingoptical autocorrelators.

The small-angle X-ray scattering intensity of ananostructured system is the Fourier transform of thespatial autocorrelation function of the electron density.

In optics, normalized autocorrelations and cross-correlations give the degree of coherence of anelectromagnetic field.

In signal processing, autocorrelation can giveinformation about repeating events like musical beats(for example, to determine tempo) or pulsar frequencies,though it cannot tell the position in time of the beat.It can also be used to estimate the pitch of a musicaltone.

In music recording, autocorrelation is used as a pitchdetection algorithm prior to vocal processing, as adistortion effect or to eliminate undesired mistakes andinaccuracies.[8]

Autocorrelation in space rather than time, via thePatterson function, is used by X-ray diffractionists tohelp recover the "Fourier phase information" on atompositions not available through diffraction alone.

Program-clc;clear all;

x = [1 5 6 3 2 7 3];N = length(x);a = x;y = x;z = x;for i = 1:N-1 for i = N-1:-1:1 x(i+1) = x(i); end x(1) = 0; z = [x;z;x];endk = z*y';m = k';subplot(2,1,1);stem(a);title('Input Sequence');ylabel('Amplitude---->');xlabel('n---->');subplot(2,1,2);stem(m);title('Autocorrelation Sequence');ylabel('Amplitude---->');xlabel('n---->');

Output-

1 2 3 4 5 6 70

2

4

6

8Input Sequence

Amplitude---->

n---->

0 2 4 6 8 10 12 140

50

100

150Autocorrelation Sequence

Amplitude---->

n---->

EXPERIMENT- 7

Aim- To perform Cross- correlation of two given sequences.

Software used- MATLAB

Theory-

In signal processing, cross-correlation is a measure ofsimilarity of two waveforms as a function of a time-lag appliedto one of them. This is also known as a sliding dot product orsliding inner-product. It is commonly used for searching a longsignal for a shorter, known feature. It has applications inpattern recognition, single particle analysis, electrontomographic averaging, cryptanalysis, and neurophysiology.

The cross-correlation is similar in nature to the convolution oftwo functions. In an autocorrelation, which is the cross-correlation of a signal with itself, there will always be a peakat a lag of zero unless the signal is a trivial zero signal. Inprobability theory and statistics, correlation is always used toinclude a standardising factor in such a way that correlationshave values between −1 and +1, and the term cross-correlation isused for referring to the correlation corr(X, Y) between tworandom variables X and Y, while the "correlation" of a randomvector X is considered to be the correlation matrix (matrix ofcorrelations) between the scalar elements of X.

Program-clc;clear all;x = [1 5 6 3 2 7 3];N = length(x);y = [2 5 7 3 2 1 4];a = x;z = x;

for i = 1:N-1 for i = N-1:-1:1 x(i+1) = x(i); end x(1) = 0; z = [x;z;x];endk = z*y';m = k'subplot(2,1,1);stem(a);title('Input Sequence');ylabel('Amplitude---->');xlabel('n---->');subplot(2,1,2);stem(m);title('Cross-correlation Sequence');ylabel('Amplitude---->');xlabel('n---->');

Output-

1 2 3 4 5 6 70

2

4

6

8Input Sequence

Amplitude---->

n---->

0 2 4 6 8 10 12 140

50

100

150Cross-correlation Sequence

Amplitude---->

n---->