0% found this document useful (0 votes)
56 views15 pages

MATLAB

The document contains MATLAB code to solve several signal processing problems: 1) Solving a system of linear equations and displaying the solution. 2) Finding the load resistance that results in maximum power transfer from a voltage source with internal resistance. 3) Generating and plotting a sequence defined by c*u(-n-n0).

Uploaded by

ilg1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views15 pages

MATLAB

The document contains MATLAB code to solve several signal processing problems: 1) Solving a system of linear equations and displaying the solution. 2) Finding the load resistance that results in maximum power transfer from a voltage source with internal resistance. 3) Generating and plotting a sequence defined by c*u(-n-n0).

Uploaded by

ilg1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

% Q1: Solve the linear equations

% 2x+3y+4z=1
% 4x+5y+ z=2
% 5x+6y+7z=3
clc;clear all;close all;
syms x1 x2 x3
eq1 = 2*x1 +3*x2 +4*x3-1;
eq2 = 4*x1 +5*x2+x3-2;
eq3 = 5*x1+6*x2+7*x3-3;
sol = solve(eq1,eq2,eq3);
double(sol.x1)
double(sol.x2)
double(sol.x3)
%

OUTPUT

% x=
% 1.0667
% y=
% -0.4667
% z=
% 0.0667

% Q2 : A voltage source of 120 V with an internal resistance of 50 ohm


% is supplying a load resistance RL.Find the value of RL that will result
% in maximum possible power being supplied by the source to the load .How
% much power be supplied to the load as a function of load resistance RL?
clc;
close all;
clear all;
V=120;
Rs=50;
RL=0:1:75;
I=V./(Rs+RL);
Power=(I.*I).*RL;
plot(RL,Power);
xlabel('RL');
ylabel('Power');
title('Power vs RL');
[Max_power,RLindex_maxp]=max(Power);
disp('Value of RL for maximum power transfer');
disp(RL(RLindex_maxp));
disp('Maximum power transferred:');
disp(Max_power);

OUTPUT

% Value of RL for maximum power transfer


% 50
% Maximum power transferred:
% 72
OUTPUT

% Q3 : Write a matlab script file to generate & plot a sequence c*u(-n-n0) where c=2 ,n0=3
clc;
close all;
clear all;
n=-10:1:10;
u=stepfun(n,3);
u_n=fliplr(u);
f=2*u_n;
stem(n,f);
xlabel('n');
ylabel('y');
title('y=2u(-n-3)');
OUTPUT

% Q4 : Plot g(n)=A(a^n)u(n) for A=10,a=0.9 for -10<=n<=20


clc;
clear all;
close all;
n=-10:1:20;
u=stepfun(n,0);
y=10*(0.9.^n).*u
stem(n,y);
xlabel('n');
ylabel('y');
title('y=A(a^n)u(n) for A=10,a=0.9');
OUTPUT

%Q5 A first order circuit with unit impulse response h(t)=exp(-t)u(t) is


%subjected to a ramp voltage of x(t)=tu(t).Assume a sampling interval of
%T=0.01 sec.Obtain y(t) at t=1 sec.
clc;
close all;
clear all;
t=0:0.01:1;
u=stepfun(t,0);
h=exp(-t).*u;
x=t.*u;
y=0.01*conv(x,h);
n=0:0.01:2;
disp('y(t) at t=1 is');
plot(n,y);
xlabel('t');
ylabel('y(t)=x(t)*h(t)');
title('y(t)=x(t)*h(t) where x(t)=e^-t*u(t),h(t)=t*u(t)');

OUTPUT
y(t) at t=1 is
0.3729

% Q6 : Write a program that plots the frequency response of a first order system
% H(z)=1/(1-0.8z^-1)
a=[1];% numerator co-efficient
b=[1 -.8];% denominator co-efficient
freqz(b,a);%plot response

OUTPUT

%Q7 For 0<=n<=10,plot the following discrete time signals in a single window
% unit step,exponential,sinusoidal,unit impulse
%Unit Step
clc;clear all;close all;
N=10;
x1=ones(1,N);
n=0:1:N-1;
subplot(2,2,1),stem(n,x1);
title('Unit Step Sequence');
%Exponenetial
x2=.6.^(n);
subplot(2,2,2);
stem(n,x2);
Title('Exponential');
%Sinusoidal
x3=.2*cos(.3*pi*n);
subplot(2,2,3),stem(n,x3);
title('Sinusoidal');
%Unit Impulse
x4=ones(1,1);
subplot(2,2,4),stem(x4);
Title('Unit Impulse');
OUTPUT

%Q8 A rectangular pulse x(n)=u(n)-u(n-10) be an input to an LTI system with


% impulse response h(n)=0.9^n*u(n).Determine y(n).Plot x(n),h(n)& y(n)
clc;
close all;
clear all;
n=0:30;
u=stepfun(n,0);
u_10=stepfun(n,10);
x=u-u_10;
h=(0.9.^n).*u;
y=conv(x,h);
l=0:60;
subplot(3,1,1);
stem(n,x,'k');
title('x(n)=u(n)-u(n-10)');
ylabel('x(n)');
subplot(3,1,2);
stem(n,h,'k');
title('h(n=0.9^n*u(n))');
ylabel('h(n)');
subplot(3,1,3);
stem(l,y,'k');
title('y(n)=x(n)*h(n)');
xlabel('n');
ylabel('y(n)');

% Q9 : A sine wave of frequency 500 Hz is sampled at a frequency of


% 5000 Hz.Evaluate its autocorrelation and crosscorrelation,and plot its power spectral density
clc;
close all;
clear all;
num=input('No of cycles');
shift=input('Phase shift(in degree)');
n=0:0.2:2*num;
x=sin(2*pi*n*0.5);
subplot(3,3,1);
plot(n,x);
title('sampled signal');
xlabel('n(ms)');
ylabel('x=sin(2pi*f*n)');
autocorrelation=xcorr(x);
n_new=0:0.2:(2*num*2);
subplot(3,3,2);
stem(n_new,autocorrelation);title('Autocorrelation');
xlabel('n');
ylabel('acorr(x)');
shift=shift*pi/180;
y=sin(2*pi*n*0.5 + shift);
subplot(3,3,3);
plot(n,y);title('Shifted signal');
xlabel('n');
ylabel('y=sin(2pi*f*n+phi)');
crosscorr=xcorr(x,y);
subplot(3,3,4);
plot(n_new,crosscorr);
xlabel('n');
ylabel('crosscorr(x,y)');
subplot(3,3,5);pxx = psd(x);plot(pxx);xlabel('Frequency');ylabel('Power');title('PSD');
OUTPUT

% Q10 :Design a 1024 tapped FIR filter with cutoff frequency of pi/2 and
% plot the requency response
clc;
close all;
clear all;
b = 0.5*sinc(0.5*(-512:512));
b = b.*hamming(1025)';
fvtool(b,1);
OUTPUT

% Q11 : A digital filter given as


%
y(n)-2.56y(n-1)+2.22y(n-2)-0.65y(n-3)=x(n)+x(n-3)
% (a)Generate a sinusoid x(n) of 500 Hz sampled at 6 KHz.Compute the output
% using filter and plot input and output on the same graph
% (b)Plot the impulse response of the filter with impz function
% (c)Generate xnew(n) as sum of 500 Hz and 1500 Hz sin wave,sampled at 6 KHz.
% (d)Plot the response with xnew(n)
clc;clear all;
a=[1 -2.56 2.22 -.65]%co-efficients of y
b=[1 0 0 1];%co-efficients of x;
n=0:1:64;
x=sin(2*pi*(500/6000)*n);%x=sin(2*pi*(f/fs)*n); n=number of samples
y=filter(b,a,x);%implement filter
subplot(2,3,1);
plot(x);title('Input Signal');
subplot(2,3,2);
plot(y);title('Output Signal');
subplot(2,3,3);
impz(b,a,64);%calculate impulse respone,64 is the number of samples
t=0:(1/6000):200/6000;
x1=sin(2*pi*500*t)+sin(2*pi*1500*t);
subplot(2,3,4);plot(t,x1);xlabel('Samples');title('sampled at 6000 Hz');
y1=filter(b,a,x1);
subplot(2,3,5);
plot(y1);title('Output through Filter');
OUTPUT

% Q12 : Design an average filter


%
y(n)=0.05[x(n)+x(n-1)+...+x(n-19)]
% (a)Generate 200 samples of 1 Hz sinewave sampled at 40 Hz.
% (b)Now,add Gaussian noise to (a).
% (c)Filter the noisy signal through 20 point moving average filter.
% (d)Plot each signal.
clc;
clear all;
a=[1];
b=.05*[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ];
n=0:1:200;
x=sin(2*pi*(1/40)*n);
subplot(2,3,1);
plot(x);title('Input Signal');
y=awgn(x,-1);
subplot(2,3,2);
plot(y);title('Input Siganl with -.1 dB Noise');
x1=filter(b,a,y);
subplot(2,3,3);
plot(x1);title('Filtered Input + Noise');
% impz(b,a,64);%calculate impulse respone,64 is the number of samples
% t=0:(1/6000):200/6000;
% x1=sin(2*pi*500*t)+sin(2*pi*1500*t);
% subplot(2,3,4);plot(t,x1);xlabel('Samples');title('sampled at 6000 Hz');
% y1=filter(b,a,x1);
% subplot(2,3,5);
% plot(y1);title('Output through Filter');
OUTPUT

%Q13 Write a program to perform linear convolution of x(n)={1,0,2,3,4} &


% h(n)={1,2,3} without using conv() function
clc;
close all;
clear all;
x=[1,0,2,3,4];
n=0:6;
h=[3,2,1,zeros(1,4)];
x1=[zeros(1,2),1,0,2,3,4];
disp('x(n)*h(n)=');
for n=1:7
y(n)=[0];
for k=1:7
y(n)=y(n)+h(k)*x1(k);
end
disp(y(n));
h=circshift(h,[0,1]);
end
%
OUTPUT
% x(n)*h(n)=
%
1
%
2
%
5
%
7
% 16
% 17
% 12

% Qsn_14 : Write a program to solve the roots of a quadratic equation


disp('Coefficients of the quadratic eqn:ax^2+bx+c=0');
a=input('a :');
b=input('b :');
c=input('c :');
d=b*b-4*a*c;
if d==0
disp('Roots are real and equal');
r1=-b/(2*a);
r2=r1;
else
if d<0
disp('Roots are imaginary and distinct');
else
disp('Roots are real and distinct');
end
end
delta=sqrt(d);
r1=(-b+delta)/(2*a);
r2=(-b-delta)/(2*a);
disp('Roots=');
disp(r1)
disp(r2)
%
OUTPUT
% CASE 1 :
% Coefficients of the quadratic eqn:ax^2+bx+c=0
% a :1
% b :2
% c :1
% Roots are real and equal
% Roots=
% -1
% -1
% CASE 2 :
% a :1
% b :-5
% c :6
% Roots are real and distinct
% Roots=
%
3
%
2
% CASE 3
% a :1
% b :1
% c :1
% Roots are imaginary and distinct
% Roots=
% -0.5000 + 0.8660i
% -0.5000 - 0.8660i

% Q15 : Create two figures of y1=e^x and y2=e^-x in the same graph for the same
%
values of x;
clc;
close all;
clear all;
t=-10:0.1:10;
y1=exp(t);
y2=exp(-t);
plot(t,y1,t,y2);
xlabel('x');
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy