Familiarize with MATLAB environment.
Learn variables, vectors, matrices, and scripts.
Generate simple mathematical functions.
clc;
clear;
t = 0:0.01:5;
x = sin(2*pi*t);
plot(t,x);
grid on;
Generate and plot:
Unit Step
Ramp
Exponential
Sinusoidal
t = -5:0.01:5;
u = t>=0;
r = t.*u;
x = exp(-t).*u;
s = sin(2*pi*t);
subplot(2,2,1);
plot(t,u); title('Unit Step');
subplot(2,2,2);
plot(t,r); title('Ramp');
subplot(2,2,3);
plot(t,x); title('Exponential');
subplot(2,2,4);
plot(t,s); title('Sine Wave');
Study:
Amplitude Scaling
Time Shifting
Time Reversal
t=-5:0.01:5;
x=sin(t);
subplot(2,2,1);
plot(t,x);
title('Original');
subplot(2,2,2);
plot(t,2*x); title('Amplitude Scaling');
subplot(2,2,3);
plot(t-2,x); title('Time Shift');
subplot(2,2,4);
plot(-t,x); title('Time Reversal');
To perform the convolution of two continuous-time signals using MATLAB and analyze the output response.
clc;
clear;
close all;
dt = 0.01;
t = -2:dt:3;
% Input signal
x = (t>=0 & t<=1);
% Impulse response
h = (t>=0 & t<=1);
% Continuous-time convolution
y = conv(x,h)*dt;
% Time axis for convolution
ty = (2*t(1)):dt:(2*t(end));
% Plot results
subplot(3,1,1)
plot(t,x,'LineWidth',2)
grid on
title('Input Signal x(t)')
xlabel('Time (s)')
ylabel('Amplitude')
subplot(3,1,2)
plot(t,h,'LineWidth',2)
grid on
title('Impulse Response h(t)')
xlabel('Time (s)')
ylabel('Amplitude')
subplot(3,1,3)
plot(ty,y,'LineWidth',2)
grid on
title('Convolution Output y(t)=x(t)*h(t)')
xlabel('Time (s)')
ylabel('Amplitude')
--------------------------------------------------------------------------
clc;
clear;
close all;
dt = 0.01;
t = -1:dt:5;
u = (t>=0);
x = exp(-t).*u;
h = u;
y = conv(x,h)*dt;
ty = (2*t(1)):dt:(2*t(end));
subplot(3,1,1)
plot(t,x,'LineWidth',2)
grid on
title('x(t)=e^{-t}u(t)')
subplot(3,1,2)
plot(t,h,'LineWidth',2)
grid on
title('h(t)=u(t)')
subplot(3,1,3)
plot(ty,y,'LineWidth',2)
grid on
title('Continuous-Time Convolution')
xlabel('Time')
Objective: To analyze periodic and aperiodic signals using Fourier series and Fourier transform and to study their frequency spectra using MATLAB.
Generate the periodic signal x(t)=2sin(2π5t)+3cos(2π10t) and plot it in MATLAB.
clear; close all; clc;
%% 1. Generation of a periodic signal
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time interval
x = 2*sin(2*pi*5*t) + 3*cos(2*pi*10*t);
figure;
plot(t, x, 'LineWidth', 1.5);
grid on;
xlabel('Time (s)');
ylabel('Amplitude');
title('x(t) = 2sin(2\pi5t) + 3cos(2\pi10t)');
Determine the trigonometric Fourier-series coefficients of a square wave with amplitude A=1 and fundamental frequency f0=5 Hz.
Reconstruct the square wave using the first:
3 harmonics
5 harmonics
10 harmonics
20 harmonics
Compare the reconstructed waveforms and observe the Gibbs phenomenon.
%% 2 and 3. Fourier-series reconstruction of a square wave
f0 = 5; % Fundamental frequency
T0 = 1/f0;
t = 0:1/fs:2*T0;
harmonicSets = [3 5 10 20];
figure;
for k = 1:length(harmonicSets)
N = harmonicSets(k);
xSquare = zeros(size(t));
% Fourier series of a unit-amplitude symmetric square wave
for n = 1:2:(2*N-1)
xSquare = xSquare + (4/pi)*(1/n)*sin(2*pi*n*f0*t);
end
subplot(2,2,k);
plot(t, xSquare, 'LineWidth', 1.4);
grid on;
ylim([-1.5 1.5]);
xlabel('Time (s)');
ylabel('Amplitude');
title([num2str(N), ' odd harmonics']);
end
sgtitle('Fourier-Series Reconstruction of a Square Wave');
4.Generate the aperiodic rectangular pulse
x(t)={1,0,∣t∣≤0.5otherwise
and calculate its Fourier transform using MATLAB.
5. Plot the magnitude and phase spectra of the rectangular pulse.
%% 4 and 5. Fourier transform of an aperiodic rectangular pulse
fs = 1000;
t = -2:1/fs:2;
% Rectangular pulse: x(t) = 1 for |t| <= 0.5
xPulse = double(abs(t) <= 0.5);
N = length(xPulse);
dt = 1/fs;
% Numerical approximation of the continuous-time Fourier transform
XPulse = fftshift(fft(ifftshift(xPulse)))*dt;
f = (-floor(N/2):ceil(N/2)-1)*(fs/N);
figure;
subplot(3,1,1);
plot(t, xPulse, 'LineWidth', 1.5);
grid on;
ylim([-0.2 1.2]);
xlabel('Time (s)');
ylabel('x(t)');
title('Aperiodic Rectangular Pulse');
subplot(3,1,2);
plot(f, abs(XPulse), 'LineWidth', 1.5);
grid on;
xlim([-10 10]);
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title('Magnitude Spectrum');
subplot(3,1,3);
plot(f, unwrap(angle(XPulse)), 'LineWidth', 1.2);
grid on;
xlim([-10 10]);
xlabel('Frequency (Hz)');
ylabel('Phase (rad)');
title('Phase Spectrum');