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

Matlab Assignment 22bec038

The document contains code for analyzing and plotting various signals including: - Plotting a sinusoidal signal sin(4πt) - Plotting unit step functions using heaviside functions - Shifting and scaling unit step functions - Calculating Fourier series coefficients for periodic signals - Taking the discrete Fourier transform (DFT) of signals and plotting the magnitude and phase spectra - Defining custom unit step and ramp functions The code generates plots of signals in both the time and frequency domains and calculates coefficients for representing signals using Fourier series expansions.

Uploaded by

Diya Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Matlab Assignment 22bec038

The document contains code for analyzing and plotting various signals including: - Plotting a sinusoidal signal sin(4πt) - Plotting unit step functions using heaviside functions - Shifting and scaling unit step functions - Calculating Fourier series coefficients for periodic signals - Taking the discrete Fourier transform (DFT) of signals and plotting the magnitude and phase spectra - Defining custom unit step and ramp functions The code generates plots of signals in both the time and frequency domains and calculates coefficients for representing signals using Fourier series expansions.

Uploaded by

Diya Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

%matlab assignment

%diya shah
% 22bec038
t=-1:0.001:1;
x=sin(4*pi*t);
figure
plot(t,x);
xticks(-1:1)
title('Signal x1(t) = sin(4*pi*t)');
xlabel('Time');ylabel('x1(t)');
grid on;

%Q1b
syms t;
y=heaviside(t)+heaviside(-t-2)+heaviside(t-2);
fplot(y);
xticks(-5:5);
yticks(-1:4);xlabel('Time');ylabel('x(t)');

1
1

ans = 1

%Q1c
syms t;
x3=sin(2*pi*t)+2*cos(2*pi*t);
fplot(x3);xlabel('Time');ylabel('Amplitude');

2
2

ans = 2

%Q2a
syms t;
x1=heaviside(t)-3*heaviside(t-2)+2*heaviside(t-4);
fplot(x1);xticks(0:5);xlabel('Time');ylabel('x(t)');

3
3

ans = 3

%Q2b
x2=heaviside(2*t)-3*heaviside(2*t-2)+2*heaviside(2*t-4);
fplot(x2);xticks(0:5);xlabel('Time');ylabel('x(t)');

4
4

ans = 4

%Q2c
syms t;
x3=heaviside((t/2))-3*heaviside((t/2)-2)+2*heaviside((t/2)-4);
fplot(x3,[-10,10]);xticks(0:10);xlabel('Time');ylabel('x(t)');

5
5

ans = 5

%Q2d
x4=heaviside(t-3)-3*heaviside((t-3)-2)+2*heaviside((t-3)-4);
fplot(x4,[0,10]);xticks(0:10);xlabel('Time');ylabel('x(t)');

6
6

ans = 6

%Q2e
x5=heaviside(t+3)-3*heaviside((t+3)-2)+2*heaviside((t+3)-4);
fplot(x5);xticks(-5:5);xlabel('Time');ylabel('x(t)');

7
7

ans = 7

%Q2f
x6=heaviside(2*t-3)-3*heaviside((2*t-3)-2)+2*heaviside((2*t-3)-4);
fplot(x6);xticks(-5:5);xlabel('Time');ylabel('x(t)');

8
8

ans = 8

%Q2g
x7=heaviside(2*t+3)-3*heaviside((2*t+3)-2)+2*heaviside((2*t+3)-4);
fplot(x5);xticks(-5:5);xlabel('Time');ylabel('x(t)');
9

ans = 9

%Q3
ti=-5:5;
sig=line(ti,3,3)+line(ti,-6,1)+line(ti,3,0)-3*step(ti,-3);

9
Operator '+' is not supported for operands of type 'matlab.graphics.primitive.Line'.

%Q-4:-
tq4 = -1:0.01:5;
xq4 = exp((1 + 1i * 2 * pi) * tq4) .* (tq4 >= 0);
real_p = real(xq4);
imag_p = imag(xq4);
figure;
plot(real_p, imag_p);
xlabel('Re{x(t)}');
ylabel('Im{x(t)}');
title('Plot of Re{x(t)} vs. Im{x(t)}');
grid on;

10
%Q-4:-
tq4 = -1:0.01:5;
xq4 = exp((1 + 1i * 2 * pi) * tq4) .* (tq4 >= 0);
real_p = real(xq4);
imag_p = imag(xq4);
figure;
plot(real_p, imag_p);
xlabel('Re{x(t)}');
ylabel('Im{x(t)}');
title('Plot of Re{x(t)} vs. Im{x(t)}');
grid on;

%Q5
n=-5:5;
im=[0,0,0,0,0,1,0,0,0,0,0];
stem(n,im);xlabel('n');ylabel('δ[n]');

1
%Q6
n2=0:100;
%if a==-0.2
for i=1:length(n2)
a1(i)=(-0.2)^n2(i);%if a==-0.2
a2(i)=(0.2)^n2(i);%if a==0.2
a3(i)=(-1.2)^n2(i);%if a==-1.2
a4(i)=(1.2)^n2(i);%if a==1.2
end
stem(n2,a1);xlabel('Number of Samples');ylabel('Amplitude');

2
stem(n2,a2);xlabel('Number of Samples');ylabel('Amplitude');

3
stem(n2,a3);xlabel('Number of Samples');ylabel('Amplitude');

4
stem(n2,a4);xlabel('Number of Samples');ylabel('Amplitude');

5
%Q7
%two finite discrete-time signals
cc = [1, 2, 3];
h = [0.5, 1, 0.5];
convolution = conv(cc, h);
disp(convolution);

0.5000 2.0000 4.0000 4.0000 1.5000

stem(convolution);xlabel('Number of Samples');
ylabel('Amplitude');

6
%Q8
syms t2;
T = 2; % Period of the signal
x_t = 2 + 4 * sin(2 * pi * t2) - 3 * cos(4 * pi * t2);
N = 10; % Number of harmonics
a0 = (1/T) * int(x_t, t2, 0, T);
an = zeros(1, N);
bn = zeros(1, N);
% Calculate the Fourier series coefficients
for n = 1:N
an(n) = (2/T) * int(x_t * cos(2*pi*n*t2/T), t2, 0, T);
bn(n) = (2/T) * int(x_t * sin(2*pi*n*t2/T), t2, 0, T);
end
disp(a0);

disp(an);

0 0 0 -3 0 0 0 0 0 0

disp(bn);

0 4 0 0 0 0 0 0 0 0

%Q9
T = 2;

7
syms t;
x_t = 2 + 4*sin(2*pi*t) - 3*cos(4*pi*t);
%Harmonics
N = 10;
coefficients = zeros(1, N);
for k = 1:N
c_k = (1/T) * int(x_t * exp(-1i*2*pi*k*t/T), t, 0, T);
coefficients(k) = c_k;
end
% Display the coefficients
for k = 1:N
fprintf("c(%d)",k);
disp(coefficients(k));
end

c(1)
0
c(2)
0.0000 - 2.0000i
c(3)
0
c(4)
-1.5000
c(5)
0
c(6)
0
c(7)
0
c(8)
0
c(9)
0
c(10)
0

%Q10a
Fs = 1000;
T10 = 1 / Fs;
L = 4000;
t10 = (0:L-1) * T10;
x110 = heaviside(t10 + 1) - heaviside(t10 - 1);
% Discrete Fourier Transform (DFT)
X1 = fft(x110);
f = (-L/2:L/2-1) * Fs / L;
mag = abs(fftshift(X1));

Phase = angle(fftshift(X1));
% magnitude spectrum
subplot(2, 1, 1);
plot(f, mag);
title('Magnitude Spectrum of x1(t)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% phase spectrum

8
subplot(2, 1, 2);
plot(f, Phase);
title('Phase Spectrum of x1(t)');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
%Q10b
tb = linspace(0, 5, 4000);
xb= exp(-2*tb) .* heaviside(tb);
X2 = fft(xb);
magb = abs(X2);
phaseb = angle(X2);
Fsb = 1 / (tb(2) - tb(1));
fb = linspace(-Fsb/2, Fsb/2, length(tb));

% magnitude spectrum
subplot(2, 1, 1);
plot(fb, fftshift(magb));
title('Magnitude Spectrum of x2(t) (Using Inbuilt Function)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% phase spectrum
subplot(2, 1, 2);
plot(fb, fftshift(phaseb));
title('Phase Spectrum of x2(t) (Using Inbuilt Function)');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');

9
function y = ustep(t,ad)
N = length(t);
y = zeros(1,N);
for i = 1:N
if t(i) >= -ad
y(i) = 1;
end

end
y = ramp(t,s,adv)
N = length(t);
y = zeros(1,N);
for i = 1:N
if t(i) >= -adv
y(i) = s * (t(i)+adv);
end
end
end

10

You might also like