0% found this document useful (0 votes)
11 views17 pages

SS Codes Graphs

Uploaded by

yarabb7860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

SS Codes Graphs

Uploaded by

yarabb7860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

clc;

close all;
clear all;

t = -10:0.01:10;

%impulse
imp_s = t == 0;

figure(1);
subplot(2,2,1);
plot(t,imp_s);
title("Impulse Signal");
xlabel("time");
ylabel("del(t)");

%step signal
step_s = t>=0;
figure(1);
subplot(2,2,2);
plot(t,step_s);
title("Unit Step");
xlabel("time");
ylabel("u(t)");

%ramp
ramp_s = t.*(t>=0);
figure(1);
subplot(2,2,3);
plot(t,ramp_s);
title("Ramp Signal");
xlabel("time");
ylabel("r(t)");

%signum
sig_s = -1.*(1.*t<=0) + (1.*t>=0);
figure(1);
subplot(2,2,4);
plot(t,sig_s);
title("Signum Signal");
xlabel("time");
ylabel("sgn(t)");

%exponential positive
exp_s = exp(t);
figure(2);
subplot(2,2,1);
plot(t,exp_s);
title("Postive Exponential Signal");
xlabel("time");
ylabel("exp(t)");

%exponential negative
exp_s = exp(-t);
figure(2);
subplot(2,2,2);
plot(t,exp_s);
title("Negative Exponential Signal");
xlabel("time");
ylabel("exp(-t)");

%sin function
sin_s = sin(t);
figure(2);
subplot(2,2,3);
plot(t,sin_s);
title("Sin Signal");
xlabel("time");
ylabel("sin(t)");

%sinc function
sinc_s = sin(pi*t)./(pi*t);
figure(2);
subplot(2,2,4);
plot(t,sinc_s);
title("Sinc Signal");
xlabel("time");
ylabel("sinc(t)");
close all;
clc;
%t = 0: 0.01: 6.28;
t = 0:0.01:1;
A = 2;
F = 4;
func = A*cos(2*pi*F*t + 60);

%oversampling
subplot(311);

plot(t,func);
title("Oversampling");
xlabel("time");
ylabel("x(t)");
fs = 3 * F;
ts = 1/fs;
n1 = 0 : ts : (fs*ts);
func2 = A*cos(2*pi*F*n1 + 60);
subplot(312);
stem(n1,func2,'r');

xlabel("time");
ylabel("x[n]");

t1 = linspace(0,max(n1),(max(n1/0.001)));
ya = interp1(n1,func2,t1,'spline');
subplot(313);
plot(t1,ya);
xlabel("time");
ylabel("x(t)");
figure;

%Undersampling
subplot(311)
plot(t,func);
title("Undersampling");
xlabel("time");
ylabel("x(t)");
fs = 1.5 * F;
ts = 1/fs;
n1 = 0 : ts : (fs*ts);
func3 = A*cos(2*pi*F*n1 + 60);

subplot(312);
stem(n1,func3,'g');

xlabel("time");
ylabel("x[n]");

t2 = linspace(0,max(n1),(max(n1/0.001)));
ya1 = interp1(n1,func3,t2,'spline');
subplot(313);
plot(t1,ya1);
xlabel("time");
ylabel("x(t)");
figure;
%proper sampling
subplot(311)
plot(t,func);
title("Proper sampling");
xlabel("time");
ylabel("x(t)");

fs = 2 * F;
ts = 1/fs;
n1 = 0 : ts : (fs*ts);
func4 = A*cos(2*pi*F*n1 + 60);

subplot(312);
stem(n1,func4,'y');

xlabel("time");
ylabel("x[n]");

t3 = linspace(0,max(n1),(max(n1/0.001)));
ya2 = interp1(n1,func4,t3,'spline');
subplot(313);
plot(t3,ya2);
xlabel("time");
ylabel("x(t)");
clc;
close all;
clear all;

n = -10:1:10;

%impulse
imp_s = n == 0;

figure(1);
subplot(2,2,1);
stem(n,imp_s);
title("Impulse Signal");
xlabel("time");
ylabel("del(n)");

%step signal
step_s = n>=0;
figure(1);
subplot(2,2,2);
stem(n,step_s);
title("Unit Step");
xlabel("time");
ylabel("u(n)");

%ramp
ramp_s = n.*(n>=0);
figure(1);
subplot(2,2,3);
stem(n,ramp_s);
title("Ramp Signal");
xlabel("time");
ylabel("r(n)");

%signum
sig_s = -1.*(1.*n<=0) + (1.*n>=0);
figure(1);
subplot(2,2,4);
stem(n,sig_s);
title("Signum Signal");
xlabel("time");
ylabel("sgn(n)");

%exponential positive
exp_s = exp(n);
figure(2);
subplot(2,2,1);
stem(n,exp_s);
title("Postive Exponential Signal");
xlabel("time");
ylabel("exp(n)");

%exponential negative
exp_s = exp(-n);
figure(2);
subplot(2,2,2);
stem(n,exp_s);
title("Negative Exponential Signal");
xlabel("time");
ylabel("exp(-n)");

%sin function
sin_s = sin(n);
figure(2);
subplot(2,2,3);
stem(n,sin_s);
title("Sin Signal");
xlabel("time");
ylabel("sin(n)");

%sinc function
sinc_s = sin(n)./(n);
figure(2);
subplot(2,2,4);
stem(n,sinc_s);
title("Sinc Signal");
xlabel("time");
ylabel("sinc(n)");
clc;
close all;

n = -10:1:10;

%og signal
ramp_s = n.*(n>=0);
subplot(331);
stem(n,ramp_s);
title("r[n]");
xlabel("n");
ylabel("r(n)");

%time shifting - right shift x[n-4]


ramp_s = (n-4).*(n>=4);
subplot(332);
stem(n,ramp_s);
title("r[n-4]");
xlabel("n");
ylabel("r(n-4)");

%time shifting - left shift x[n+4]


ramp_s = (n+4).*(n>=-4);
subplot(333);
stem(n,ramp_s);
title("r[n+4]");
xlabel("n");
ylabel("r(n+4)");

%time reversal
ramp_s = -1.*n.*(n<=0);
subplot(334);
stem(n,ramp_s);
title("r[-n]");
xlabel("n");
ylabel("r(-n)");

%time scaling - expansion


ramp_s = (n).*((n>=0));
subplot(335);
stem(n./2,ramp_s);
title("r[n/2]");
xlabel("n");
ylabel("r(n/2)");

%time scaling - compression


ramp_s = (n).*((n>=0));
subplot(336);
stem(n.*2,ramp_s);
title("r[2*n]");
xlabel("n");
ylabel("r(2*n)");

%amplitude scaling - positive


ramp_s = 2.*(n).*((n>=0));
subplot(337);
stem(n,ramp_s);
title("2*r[n]");
xlabel("n");
ylabel("2*r(n)");

%amplitude scaling - negative


ramp_s = -2.*(n).*((n>=0));
subplot(338);
stem(n,ramp_s);
title("-2*r[n]");
xlabel("n");
ylabel("-2*r(n)");
n = -10:1:10;
x = [1 2 3];
h = [1 2 3 4];
L = length(x) + length(h) - 1;
x1 = [x zeros(1,L - length(x))];
h1 = [h zeros(1,L - length(h))];
answ = zeros(1,L);
for i = 1 : L
h1_idx = i;
for j = 1 : i
answ(i) = answ(i) + h1(h1_idx)*x1(j);
h1_idx = h1_idx - 1;
end
end

ans_func = conv(x,h);

subplot(4,1,1);
stem(x);
ylabel("x(n)");
xlabel("n");

subplot(4,1,2);
stem(h);
ylabel("h(n)");
xlabel("n");

subplot(4,1,3);
stem(answ);
title("Convolution using loop");
ylabel("y(n)");
xlabel("n");

subplot(4,1,4);
stem(ans_func);
title("Convolution using in-built function");
ylabel("y(n)");
xlabel("n");
% FR of rect pulse
%generate the rectangle pulse
t = -5:0.001:5;
L = length(t);

for i = 1 : L
if t(i) >= -2 && t(i) <=2
x(i)= 1;
else
x(i)= 0;
end
end
subplot(411);
plot(t,x);
title("Rectangular Pulse")
xlabel('Time');
ylabel('Amplitude');

t = -2:0.001:2;
k = 0;

for f = -5:0.001:5
k = k+1;
X(k) = trapz(t,exp((-1i)*2*pi*f*t));

end
f = -5:0.001:5;
subplot(412);
plot(f,X);
title("Amplitude Spectrum")
xlabel('Freq');
ylabel('Amplitude');

%Plotting magnitude and phase


%for mag use abs() function
subplot(413);
plot(f,abs(X));
title("Magnitude Spectrum")
xlabel('freq');
ylabel('Magnitude');

%for phase use angle() function


subplot(414);
plot(f,angle(X));
title("Phase Spectrum")
xlabel('freq');
ylabel('Phase');
t = -5:0.001:5;
fc = 2;
y1 = sin(2*pi*fc*t);

subplot(411);
plot(t,y1);
title("Sin Function")
xlabel('time');
ylabel('Amplitude');

t = -5:0.001:5;
k = 0;

for f = -5:0.001:5
k = k+1;
X(k) = trapz(y1,exp((-1i)*2*pi*f*t));

end
f = -5:0.001:5;
subplot(412);
plot(f,X);
title("Amplitude Spectrum")
xlabel('Freq');
ylabel('Amplitude');

%Plotting magnitude and phase


%for mag use abs() function
subplot(413);
plot(f,abs(X));
title("Magnitude Spectrum")
xlabel('freq');
ylabel('Magnitude');

%for phase use angle() function


subplot(414);
plot(f,angle(X));
title("Phase Spectrum")
xlabel('freq');
ylabel('Phase');
t = -5:0.001:5;

fc = 2;
y1 = cos(2*pi*fc*t);

subplot(411);
plot(t,y1);
title("Cos Function")
xlabel('time');
ylabel('Amplitude');

t = -5:0.001:5;
k = 0;

for f = -5:0.001:5
k = k+1;
X(k) = trapz(y1,exp((-1i)*2*pi*f*t));

end
f = -5:0.001:5;
subplot(412);
plot(f,X);
title("Amplitude Spectrum")
xlabel('Freq');
ylabel('Amplitude');

%Plotting magnitude and phase


%for mag use abs() function
subplot(413);
plot(f,abs(X));
title("Magnitude Spectrum")
xlabel('freq');
ylabel('Magnitude');

%for phase use angle() function


subplot(414);
plot(f,angle(X));
title("Phase Spectrum")
xlabel('freq');
ylabel('Phase');

You might also like