SS Lab
SS Lab
Generate various Signals and Sequences: Periodic and Aperiodic, Unit Impulse, Unit
Step, Square, Saw tooth, Triangular, Sinusoidal, Ramp, Sinc function
MATLAB Code:
1. Unit Impulse
n = -10:10;
unit_impulse = n == 0;
figure;
stem(n, unit_impulse);
title('Unit Impulse Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;
2. Unit Step
n = -10:10;
unit_step = n >= 0;
figure;
stem(n, unit_step);
title('Unit Step Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;
3. Square Wave:
t = 0:0.01:1;
square_wave = square(2 * pi * 5 * t);
figure;
plot(t, square_wave);
title('Square Wave');
xlabel('t');
ylabel('Amplitude');
grid on;
4. Sawtooth Wave:
t = 0:0.01:1;
sawtooth_wave = sawtooth(2 * pi * 5 * t);
figure;
plot(t, sawtooth_wave);
title('Sawtooth Wave');
xlabel('t');
ylabel('Amplitude');
grid on;
5. Triangular Wave:
t = 0:0.01:1;
triangular_wave = sawtooth(2 * pi * 5 * t, 0.5);
figure;
plot(t, triangular_wave);
title('Triangular Wave');
xlabel('t');
ylabel('Amplitude');
grid on;
6. Sinusoidal Wave:
t = 0:0.01:1;
sinusoidal_wave = sin(2 * pi * 5 * t);
figure;
plot(t, sinusoidal_wave);
title('Sinusoidal Wave');
xlabel('t');
ylabel('Amplitude');
grid on;
7. Ramp Function:
t = -10:10;
ramp_function = t .* (t >= 0);
figure;
plot(t, ramp_function);
title('Ramp Function');
xlabel('t');
ylabel('Amplitude');
grid on;
8. Sinc Function:
t = -10:0.01:10;
sinc_function = sinc(t);
figure;
plot(t, sinc_function);
title('Sinc Function');
xlabel('t');
ylabel('Amplitude');
grid on;
9. Periodic Signal:
t = 0:0.01:1;
periodic_signal = sin(2 * pi * 5 * t) + sin(2 * pi * 10 * t);
figure;
plot(t, periodic_signal);
title('Periodic Signal');
xlabel('t');
ylabel('Amplitude');
grid on;
10.Aperiodic Signal
t = -5:0.01:5;
aperiodic_signal = exp(-t.^2);
figure;
plot(t, aperiodic_signal);
title('Aperiodic Signal');
xlabel('t');
ylabel('Amplitude');
grid on;
Output Graphs:
Experiment 2:
MATLAB Code:
Here's a MATLAB script that demonstrates various operations on signals and
sequences, including addition, multiplication, scaling, shifting, folding, and the computation
of energy and average power.
% Addition of signals
x_add = x1 + x2;
% Multiplication of signals
x_mult = x1 .* x2;
% Scaling of signals
a = 2; % Scaling factor
x_scaled = a * x1;
% Shifting of signals
k = 5; % Shift amount
x_shifted = circshift(x1, k);
subplot(3, 2, 3);
stem(n, x_add, 'k');
title('Addition of x1 and x2');
xlabel('n');
ylabel('x1[n] + x2[n]');
subplot(3, 2, 4);
stem(n, x_mult, 'g');
title('Multiplication of x1 and x2');
xlabel('n');
ylabel('x1[n] * x2[n]');
subplot(3, 2, 5);
stem(n, x_scaled, 'm');
title(['Scaling of x1 by ', num2str(a)]);
xlabel('n');
ylabel('a * x1[n]');
subplot(3, 2, 6);
stem(n, x_shifted, 'c');
title(['Shifting of x1 by ', num2str(k), ' samples']);
xlabel('n');
ylabel('x1[n-k]');
figure;
subplot(2, 1, 1);
stem(n, x_folded, 'y');
title('Folding (Time-reversal) of x1');
xlabel('n');
ylabel('x1[-n]');
Output :
Energy of x1: 10
Average power of x1: 0.47619
Output Graphs:
Experiment 3:
Write a program to find the trigonometric & exponential Fourier series coefficients
of a rectangular periodic signal. Reconstruct the signal by combining the Fourier
series coefficients with appropriate weightings- Plot the discrete spectrum of the
signal.
Matlab Code:
% Time vector
t = linspace(-T, T, 1000);
for n = 1:N
% Compute the coefficients a_n and b_n for trigonometric Fourier series
an(n) = (2/T) * integral(@(t) x(t).*cos(n*omega0*t), -T/2, T/2);
bn(n) = (2/T) * integral(@(t) x(t).*sin(n*omega0*t), -T/2, T/2);
end
for k = -N:N
cn(k + N + 1) = (1/T) * integral(@(t) x(t).*exp(-1i*k*omega0*t), -T/2, T/2);
end
Output:
Fourier series coefficients (a_n):
0.4502 0.3183 0.1501 0.0000 -0.0900 -0.1061 -0.0643 -0.0000 0.0500 0.0637
0.0000 -0.0396 0.1366 -0.0000 0.0000 -0.0304 -0.1432 -0.0002 -0.0737 0.0651
0.0318 + 0.0000i 0.0250 + 0.0000i -0.0000 + 0.0000i -0.0322 - 0.0000i -0.0531 + 0.0000i
Columns 6 through 10
-0.0450 + 0.0000i 0.0000 + 0.0000i 0.0750 - 0.0000i 0.1592 + 0.0000i 0.2251 + 0.0000i
Columns 11 through 15
0.2500 + 0.0000i 0.2251 - 0.0000i 0.1592 - 0.0000i 0.0750 + 0.0000i 0.0000 - 0.0000i
Columns 16 through 20
-0.0450 - 0.0000i -0.0531 - 0.0000i -0.0322 + 0.0000i -0.0000 - 0.0000i 0.0250 - 0.0000i
Column 21
0.0318 - 0.0000i
Output Graphs:
Experiment 4:
Write a program to find Fourier transform of a given signal. Plot its amplitude and
phase spectrum.
Here's a MATLAB script that computes the Fourier transform of a given signal and plots its
amplitude and phase spectrum:
MATLAB Code:
% Define the signal
t = -5:0.01:5; % Time vector
x = @(t) rectpuls(t, 2); % Example signal: Rectangular pulse of width 2
subplot(3, 1, 1);
plot(t, x_t);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
Output:
Fourier Transform Properties:
Max Amplitude: 200
Min Phase: -3.1353
Max Phase: 3.1353
Output Graphs:
Experiment 5:
Write a program to convolve two discrete time sequences. Plot all the sequences.
Here's a MATLAB script that convolves two discrete-time sequences and plots the original
sequences along with their convolution result
MATLAB Code:
% Perform convolution
y = conv(x1, x2);
subplot(3, 1, 1);
stem(n1, x1, 'b', 'filled');
title('Sequence x1[n]');
xlabel('n');
ylabel('x1[n]');
grid on;
subplot(3, 1, 2);
stem(n2, x2, 'r', 'filled');
title('Sequence x2[n]');
xlabel('n');
ylabel('x2[n]');
grid on;
subplot(3, 1, 3);
stem(ny, y, 'k', 'filled');
title('Convolution Result y[n] = x1[n] * x2[n]');
xlabel('n');
ylabel('y[n]');
grid on;
Output Graphs:
Experiment 6:
Here is a MATLAB script that computes and plots the autocorrelation and cross-correlation
of two given sequences.
MATLAB Code:
% Compute autocorrelation of x1
auto_corr_x1 = xcorr(x1);
% Compute autocorrelation of x2
auto_corr_x2 = xcorr(x2);
subplot(3, 2, 1);
stem(n1, x1, 'b', 'filled');
title('Sequence x1[n]');
xlabel('n');
ylabel('x1[n]');
grid on;
subplot(3, 2, 2);
stem(n2, x2, 'r', 'filled');
title('Sequence x2[n]');
xlabel('n');
ylabel('x2[n]');
grid on;
subplot(3, 2, 3);
stem(n_auto_x1, auto_corr_x1, 'b', 'filled');
title('Autocorrelation of x1[n]');
xlabel('Lag');
ylabel('R_{x1x1}[k]');
grid on;
subplot(3, 2, 4);
stem(n_auto_x2, auto_corr_x2, 'r', 'filled');
title('Autocorrelation of x2[n]');
xlabel('Lag');
ylabel('R_{x2x2}[k]');
grid on;
subplot(3, 2, 5:6);
stem(n_cross, cross_corr, 'k', 'filled');
title('Cross-Correlation between x1[n] and x2[n]');
xlabel('Lag');
ylabel('R_{x1x2}[k]');
grid on;
Output Graph:
Experiment 7:
Write a program to verify Linearity and Time Invariance properties of a given
Continuous System.
MATLAB Code:
% Define the continuous system as a function
% Example: A simple system where y(t) = 2 * x(t)
system = @(t, x) 2 * x;
% Verify linearity
linearity_error = max(abs(y_combined - y_combined_expected));
% Plot results
figure;
subplot(3, 2, 1);
plot(t, x1(t), 'b');
title('Input x1(t)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(3, 2, 2);
plot(t, y1, 'r');
title('Output y1(t)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(3, 2, 3);
plot(t, x2(t), 'b');
title('Input x2(t)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(3, 2, 4);
plot(t, y2, 'r');
title('Output y2(t)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(3, 2, 5);
plot(t, x_combined(t), 'b');
title('Combined Input a1*x1(t) + a2*x2(t)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(3, 2, 6);
plot(t, y_combined, 'r', t, y_combined_expected, 'g--');
title('Combined Output vs Expected Output');
xlabel('Time');
ylabel('Amplitude');
legend('Combined Output', 'Expected Output');
grid on;
figure;
subplot(2, 2, 1);
plot(t, x1_shifted(t), 'b');
title('Shifted Input x1(t-\tau)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(2, 2, 2);
plot(t, y1_shifted, 'r', t, y1_expected_shifted, 'g--');
title('Shifted Output vs Expected Shifted Output for x1(t)');
xlabel('Time');
ylabel('Amplitude');
legend('Shifted Output', 'Expected Output');
grid on;
subplot(2, 2, 3);
plot(t, x2_shifted(t), 'b');
title('Shifted Input x2(t-\tau)');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(2, 2, 4);
plot(t, y2_shifted, 'r', t, y2_expected_shifted, 'g--');
title('Shifted Output vs Expected Shifted Output for x2(t)');
xlabel('Time');
ylabel('Amplitude');
legend('Shifted Output', 'Expected Output');
grid on;
Output:
System is linear.
System is not time-invariant.
Output Graphs:
Experiment 8:
Write a program to generate discrete time sequence by sampling a continuous time
signal. Show that with sampling rates less than Nyquist rate, aliasing occurs while
reconstructing the signal.
MATLAB Code:
% Define the continuous-time signal
Fs = 1000; % Sampling frequency for the original continuous-time signal
t = 0:1/Fs:1; % Time vector
f0 = 50; % Frequency of the continuous-time signal
x_continuous = sin(2*pi*f0*t); % Continuous-time signal
n2 = 0:1/Fs2:1;
x_sampled2 = sin(2*pi*f0*n2);
% Plot the sampled signal and reconstructed signal (above Nyquist rate)
subplot(3, 1, 2);
stem(n1, x_sampled1, 'r');
hold on;
plot(t, x_reconstructed1, 'g--');
title('Sampling and Reconstruction (Above Nyquist Rate)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Sampled Signal', 'Reconstructed Signal');
grid on;
% Plot the sampled signal and reconstructed signal (below Nyquist rate)
subplot(3, 1, 3);
stem(n2, x_sampled2, 'r');
hold on;
plot(t, x_reconstructed2, 'g--');
title('Sampling and Reconstruction (Below Nyquist Rate)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Sampled Signal', 'Reconstructed Signal');
grid on;
Output :
Original sampling rate: 1000 Hz
Above Nyquist sampling rate: 100 Hz
Below Nyquist sampling rate: 30 Hz
Output Graphs:
Experiment 9:
Write a program to generate Complex Gaussian noise and find its mean, variance,
Probability Density Function (PDF) and Power Spectral Density (PSD).
MATLAB Code:
n2 = 0:1/Fs2:1;
x_sampled2 = sin(2*pi*f0*n2);
% Plot the sampled signal and reconstructed signal (above Nyquist rate)
subplot(3, 1, 2);
stem(n1, x_sampled1, 'r');
hold on;
plot(t, x_reconstructed1, 'g--');
title('Sampling and Reconstruction (Above Nyquist Rate)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Sampled Signal', 'Reconstructed Signal');
grid on;
% Plot the sampled signal and reconstructed signal (below Nyquist rate)
subplot(3, 1, 3);
stem(n2, x_sampled2, 'r');
hold on;
plot(t, x_reconstructed2, 'g--');
title('Sampling and Reconstruction (Below Nyquist Rate)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Sampled Signal', 'Reconstructed Signal');
grid on;
% Calculate PDF
[real_pdf, real_x] = ksdensity(real(complex_noise)); % Real part
[imag_pdf, imag_x] = ksdensity(imag(complex_noise)); % Imaginary part
% Plot PDF
figure;
subplot(2, 1, 1);
plot(real_x, real_pdf, 'b');
hold on;
plot(imag_x, imag_pdf, 'r');
title('Probability Density Function (PDF)');
xlabel('Value');
ylabel('Density');
legend('Real Part', 'Imaginary Part');
grid on;
% Plot PSD
subplot(2, 1, 2);
plot(freq, 10*log10(psd), 'k');
title('Power Spectral Density (PSD)');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;