0% found this document useful (0 votes)
43 views

X 1:10 ( (X 1) && (X 4) ) y (X) 2 (X 5) y (X) 4 y (X) 0 : Output

This document contains MATLAB code that defines and plots various signal processing functions including unit step functions, unit impulse functions, unit ramp functions, and convolution of signals both using the conv() function and without using the conv() function. It also contains code that generates a signal as the sum of two sinusoids, corrupts it with noise, and plots the time domain signal and its frequency spectrum.

Uploaded by

Baljinder Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

X 1:10 ( (X 1) && (X 4) ) y (X) 2 (X 5) y (X) 4 y (X) 0 : Output

This document contains MATLAB code that defines and plots various signal processing functions including unit step functions, unit impulse functions, unit ramp functions, and convolution of signals both using the conv() function and without using the conv() function. It also contains code that generates a signal as the sum of two sinusoids, corrupts it with noise, and plots the time domain signal and its frequency spectrum.

Uploaded by

Baljinder Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PLOT THE FUNCTION x(n)={ 2

4
0
CODE
for x=1:10
if((x>1)&&(x<4))
y(x)=2;
elseif(x==5)
y(x)=4;
else
y(x)=0;
end
end
x=1:10;
stem(x,y)

OUTPUT

;1<n<4
;n=5
;elsewhere

UNIT STEP FUNCTION


function y=dsp3step(a,b,s)
n=a:b;
y=(n>=s);
end
UNIT IMPULSE FUNCTION
function y=impulse(a,b,s)
n=a:b;
y=(n==s);
end
UNIT RAMP FUNCTION
function y=ramp(a,b,s)
n=a:b;
y=((n-s).*(n>=s));
end

OUTPUT
x(t)=dsp3step(0,10,1)-2*dsp3step(0,10,0)+4*dsp3step(0,10,1)

x(t)=ramp(0,10,2)-ramp(0,10,1)+4*dsp3step(0,10,-1)

x(t)=impulse(0,20,3)-4*impulse(0,20,-5)+2*impulse(0,20,0)

Convolution using conv() function


x1=input('Enter lower limit')
x2=input('Enter upper limit')
x3=x1:x2;
x4=input('Signal 1')
subplot(3,1,1)
stem(x3,x4)
x5=input('Enter lower limit')
x6=input('Enter upper limit')
x7=x5:x6;
x8=input('Signal 1')
subplot(3,1,2)
stem(x7,x8)
lm=(x1+x5);
um=(x2+x6);
x9=lm:um;
y=conv(x4,x8)
subplot(3,1,3)
stem(x9,y)

OUTPUT

Convolution without using conv() function


x1=input('Enter lower limit ')
x2=input('Enter upper limit ')
x3=x1:x2;
x4=input('Signal 1 ')
subplot(3,1,1)
stem(x3,x4)
x5=input('Enter lower limit ')
x6=input('Enter upper limit ')
x7=x5:x6;
x8=input('Signal 2 ')
subplot(3,1,2)
stem(x7,x8)
lm=(x1+x5);
um=(x2+x6);
x9=lm:um;
m=length(x4);
n=length(x8);
l=m+n-1;
for j=0:l
for i=max(1,j+1-n):min(j,m)
w(j)=x4(i)*x8(j+1-i);
end
end
subplot(3,1,3)
stem(x9,w)

OUTPUT

FREQUENCY SPECTRUM OF A SIGNAL


Fs = 1000;
% Sampling frequency
T = 1/Fs;
% Sample time
L = 1000;
% Length of signal
t = (0:L-1)*T;
% Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t));
% Sinusoids plus noise
subplot(2,1,1)
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2)
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

OUTPUT

You might also like