Program 1 Aim

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

PROGRAM 1

AIM:

What Is MATLAB

MATLAB is a high-performance language for technical computing. It integrates


computation, visualization, and programming in an easy-to-use environment where
problems and solutions are expressed in familiar mathematical notation. Typical uses
include Math and computation Algorithm development Data acquisition Modeling,
simulation, and prototyping Data analysis, exploration, and visualization Scientific and
engineering graphics Application development, including graphical user interface
building MATLAB is an interactive system whose basic data element is an array that
does not require dimensioning. This allows you to solve many technical computing
problems, especially those with matrix and vector formulations, in a fraction of the time it
would take to write a program in a scalar no interactive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to
provide easy access to matrix software developed by the LINPACK and EISPACK
projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries,
embedding the state of the art in software for matrix computation. MATLAB has evolved
over a period of years with input from many users. In university environments, it is the
standard instructional tool for introductory and advanced courses in mathematics,
engineering, and science. In industry, MATLAB is the tool of choice for high-productivity
research, development, and analysis. MATLAB features a family of add-on application-
specific solutions called toolboxes. Very important to most users of MATLAB, toolboxes
allow you to learn and apply specialized technology. Toolboxes are comprehensive
collections of MATLAB functions (M-files) that extend the MATLAB environment to solve
particular classes of problems. Areas in which toolboxes are available include signal
processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and
many others

Basics of Matlab:

Matlab windows:

Matlab works through four basic windows:

(1) MATLAB Desktop: This consist of following sub windows:

(A) COMMAND WINDOWS: This is main window. It is characterized by MATLAB


command prompt (>>)
(B) CURRENT DIRECTORY: This is where all your files currently in use are listed
you can run M files, rename them, delete them etc.

(C) WORKSHOP: This sub window lists all variable that you have generated so far
and shows their type and size.

(D) COMAND HISTORY: All command typed on MATLAB prompt in the command
window get recorded in this window.

2.FIGURE WINDOW :the output of all graphics command typed in the command
window are flushed to the graphics or the figure window , a separate gray window with
(default) white back ground color

3. EDITOR WINDOW:

This is where user writes, edit, create and save his program in program files called m
files

User can use any text editor to carry out these tasks

FILETYPES;

Matlab can read and write several types of files .here mainly 5 types of files for storing
data and programs

1. MFILES: These standards ASCII. Text files ,with ,m extension to the files name
2. MAT files: These are binary data files with .MAT extension to the file name, mat
file are created by mat lab where user save command
3. FIG- these are binary figure files with .fig extension that can be opened again as
figures
4. PFILES; these are compiled m files with .p extension that can be executed in
matlab directory these files are created with pcode command.
5. MEX-FILES; these are matlab capable FORTAN & C program with .mex
extension to the files names

COMMANDS IN MATLAB:

SUBPLOT: Subplot divides the current figure into rectangular panes that are numbered
row wise. Each pane contains an axes. Subsequent plots are output to the current
pane. Subplot (m,n,p) creates an axes in the pth pane of a figure divided into an m-by-n
matrix of rectangular panes. The new axes becomes the current axes. If p is a vector, it
specifies an axes having a position that covers all the subplot positions listed in p.
subplot(m,n,p,'replace') If the specified axes already exists, delete it and create a new
axes. subplot(m,n,p,'align') positions the individual axes so that the plot boxes align, but
does not prevent the labels and ticks from overlapping. subplot(h) makes the axes with
handle h current for subsequent plotting commands. subplot('Position',[left bottom width
height]) creates an axes at the position specified by a four-element vector. left, bottom,
width, and height are in normalized coordinates in the range from 0.0 to 1.0. h =
subplot(...) returns the handle to the new axes.

DISP: disp(X) displays an array, without printing the array name. If X contains a text
string, the string is displayed. Another way to display an array on the screen is to type
its name, but this prints a leading "X =," which is not always desirable. Note that disp
does not display empty arrays.

STEM: A two-dimensional stem plot displays data as lines extending from a baseline
along the x-axis. A circle (the default) or other marker whose y-position represents the
data value terminates each stem. stem(Y) plots the data sequence Y as stems that
extend from equally spaced and automatically generated values along the x-axis. When
Y is a matrix, stem plots all elements in a row against the same x value.

CLC: clc clears all input and output from the Command Window display, giving you a
"clean screen." After using clc, you cannot use the scroll bar to see the history of
functions, but you still can use the up arrow to recall statements from the command
history.

EXP: The exp function is an elementary function that operates element-wise on arrays.
Its domain includes complex numbers

PLOT: plot(Y) plots the columns of Y versus their index if Y is a real number. If Y is
complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the
imaginary component is ignored. plot(X1,Y1,...) plots all lines defined by Xn versus Yn
pairs. If only Xn or Yn is a matrix, the vector is plotted versus the rows or columns of the
matrix, depending on whether the vector's row or column dimension matches the matrix.

XLABEL: xlabel (‘string’) labels the x axis of current axis

YLABEL: ylabel (‘string’) labels the y axis of current axis.


PROGRAM 2

% impulse function %
clc;
clear all;
a=input('enter the range');
t=-a:1:a;
y=[zeros(1,a),ones(1),zeros(1,a)];
subplot(3,3,1);
stem(t,y);
xlabel('time');
ylabel('amplitude');

% unit function %
clc;
clear all;
a=input('enter the range');
t=-a:1:a;
y=[zeros(1,a),ones(1,a+1)];
subplot(3,3,3);
stem(t,y);
xlabel('time');
ylabel('amplitude');

% discrete ramp function %


clc;
clear all;
a=input('enter the range');
t=-a:1:a;
y=t;
for i=1:a
y(1,i)=0;
end
subplot(3,3,5);
stem(t,y);
xlabel('time');
ylabel('amplitude');

% sinusodial function %
clc;
clear all;
t=0:pi/10:2*pi;
y=sin(t);
subplot(3,3,7);
plot(t,y);
set(gca,'xtick',0:pi:2*pi);
set(gca,'XTickLabel',{'0','pi','2pi'})
xlabel('time');
ylabel('amplitude');

%exponentialfunction%
a=input('enter the value of a');
n2=input('enter the value of n2');
t=0:1:n2-1;
y=exp(a*t);
subplot(3,3,9);
stem(t,y);
title('exponential function')
xlabel('time');
ylabel('amplitude');

INPUT:-

Enter the range 5


Enter the range 5
Enter the range 5
Enter the value of a 1
Enter the value of n2 10
OUTPLOT:-
PROGRAM 3
AIM:
%convolution of two sequences%
x=input('enter the first sequece');
h=input('enter the second sequence');
a=length(x);
b=length(h);
c=a+b-1;
y=conv(x,h);
n=0:1:c-1;
disp('output sequence');
disp(y);

subplot(1,3,1);
stem(x);
title('first sequence');
xlabel('time');
ylabel('amplitude');

subplot(1,3,2);
stem(h);
title('second sequence');
xlabel('time');
ylabel('amplitude');

subplot(1,3,3);
stem(y);
title('convolution sequence');
xlabel('time');
ylabel('amplitude');

INPUT::
enter the first sequece[1 2 3 4 5]
enter the second sequence[1 2 3 4 5]
output sequence
1 4 10 20 35 44 46 40 25
OUTPUT PLOTS:
PROGRAM 4
AIM:
%crosscorelation%
clc;
x=input('enter the first sequence');
h=input('enter the second sequence');
y=xcorr(x,h);

disp('output sequence');
disp(y);
subplot(3,1,1);
stem(x);
title('first sequence');
xlabel('time');
ylabel('amplitude');
subplot(3,1,2);
stem(h);
title('second sequence');
xlabel('time');
ylabel('amplitude');

subplot(3,1,3);
stem(y);
title('crosscorelation sequence');
xlabel('time');
ylabel('amplitude');

INPUTS::
enter the first sequence[1 2 3 4 5]
enter the second sequence[2 4 6 8 10]
output sequence
10.0000 28.0000 52.0000 80.0000 110.0000 80.0000 52.0000 28.0000
10.0000

OUTPUT PLOTS:
PROGRAM 5
AIM:
%auto correlation%
clc;
x=input('enter the first sequence');
y=xcorr(x,x);
disp('output sequence');
disp(y);
subplot(3,1,1);
stem(x);
title('input sequence');
xlabel('time');
ylabel('amplitude');
subplot(3,1,2);
stem(y);
title('autocorrelation sequence');
xlabel('time');
ylabel('amplitude');

INPUTS::
enter the first sequence[3 2 1 4 5]
output sequence
15.0000 22.0000 16.0000 32.0000 55.0000 32.0000 16.0000 22.0000
15.0000

OUTPUT PLOTS:
PROGRAM 6
AIM:
%buterworth low pass filter%
clc;
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband freq');
fs=input('enter the sampling band freq');

w1=2*wp/fs;
w2=2*ws/fs;

[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn,'low');

w=0:.01:pi;
[h,om]=freqz(b,a,w);

m=20*log10(abs(h));

an=angle(h);

subplot(2,1,1);
plot(om/pi,m);
title('response of buterworth low passfiler');
ylabel('gain in DB');
xlabel('normalized freq');

subplot(2,1,2);
plot(om/pi,an);
xlabel('normalized angle');
ylabel('phase in radians');

INPUTS::
enter the passband ripple0.15
enter the stopband ripple60
enter the passband frequency1500
enter the stopband freq3000
enter the sampling band freq7000
OUTPUT PLOTS:

PROGRAM 7
AIM:
%buterworth high pass filter%
clc;
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband freq');
fs=input('enter the sampling band freq');

w1=2*wp/fs;
w2=2*ws/fs;

[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn,'high');

w=0:.01:pi;
[h,om]=freqz(b,a,w);

m=20*log10(abs(h));

an=angle(h);

subplot(2,1,1);
plot(om/pi,m);
title('response of buterworth high passfilter');
ylabel('gain in DB');
xlabel('normalized freq');

subplot(2,1,2);
plot(om/pi,an);
xlabel('normalized angle');
ylabel('phase in radians');

INPUT::
enter the passband ripple0.2
enter the stopband ripple40
enter the passband frequency2000
enter the stopband freq3500
enter the sampling band freq8000
OUTPUT PLOTS:
PROGRAM 8
AIM:
%FFT%
clc;
x=input('enter the sequence');
n=input('enter the length of fft');
y=fft(x,n);
stem(y,x);
ylabel('imaginary axis---->');
xlabel('real axix----->');

INPUT::
enter the sequence[0 1 2 3 4 5 6 7]
enter the length of fft 8

OUTPUT::
PROGRAM 9
AIM: write a program for the real and complex exponential sequences.

%real and complex exponential sequences%


clc;
k=input('enter the value of gain,k:');
a=input('enter the value of a,a:');
N=input('enter the value of N:');

n=0:1:N;
y=k*(a.^n);

subplot(3,1,1);
stem(n,y);
xlabel('time');
ylabel('amplitude');

p=input('enter the value of gain,p:');


a1=input('enter the value of real part,a1:');
b1=input('enter the value of imaginary part,b1:');
N1=input('enter the value of N1');

n1=0:1:N1;

c=a1+b1*j;

x=p*(c.^n1);

subplot(3,1,2);
stem(n1,real(x));
xlabel('time');
ylabel('amplitude');

subplot(3,1,3);
stem(n1,imag(x));
xlabel('time');
ylabel('amplitude');

disp('result is');
disp(x);
INPUT::
enter the value of gain,k:2
enter the value of a,a:2
enter the value of N:7
enter the value of gain,p:1
enter the value of real part,a1:3
enter the value of imaginary part,b1:2
enter the value of N14
result is
1.0e+002 *

0.0100 0.0300 + 0.0200i 0.0500 + 0.1200i -0.0900 + 0.4600i -1.1900 +


1.2000i

OUTPUT PLOT:

PROGRAM 10:
AIM: write a program to design chebyshev low pass filter.

%chebyshev low pass filter%


clc;
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband freq');
fs=input('enter the sampling band freq');

w1=2*wp/fs;
w2=2*ws/fs;

[n,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn);

w=0:.01:pi;
[h,om]=freqs(b,a,w);

m=20*log10(abs(h));

an=angle(h);

subplot(2,1,1);
plot(om/pi,m);
title('response of chebyshev low passfiler');
ylabel('gain in DB');
xlabel('normalized freq');

subplot(2,1,2);
plot(om/pi,an);
xlabel('normalized angle');
ylabel('phase in radians');

INPUTS::

enter the passband ripple .6


enter the stopband ripple 45
enter the passband frequency 1000
enter the stopband freq 2500
enter the sampling band freq 12000

OUTPUT PLOT:

PROGRAM 11
AIM; Write a program to define a function to compute dtft of finite length signal. Plot the
magnitude and phase plots using sub plots.Use this function to obtain dtft of a 21 point
Triangular pulse over the domain -10<n<10.plot the result over -pi<w<pi using MATLAB.

Step1(function):-

%function[magx,angx,k,]=dtftf(n,x);

k=-500:500;

w=(pi/500)*k;

X=x*(exp(-j*pi/500)).^(n'*k);

magX=abs(X);

angX=angle(X);

Step2(using defined function to compute dtft):-

n=[-10:10];

x=n;

%[magX,angX,k]=dtftf(n,x);

subplot(2,1,1);

plot(k/500,magX);

xlabel('frequency');

ylabel('magnitude');

title('dtft of a finite length sequence');

subplot(2,1,2);

plot(k/500,angX);

xlabel('frequency');

ylabel('angle');
OUTPUT PLOTS:-

You might also like