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

DSP LAB Lab 1

DSP LAB on signal generation

Uploaded by

Raihan Khan
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)
4 views

DSP LAB Lab 1

DSP LAB on signal generation

Uploaded by

Raihan Khan
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/ 9

January 17, 2025

Lab 1: Generation of basic signals.

Code: Matlab
clc;
clear;
close all;

% Time vector
t = -1:0.001:1; % Time from -1 to 1 seconds with a step of 0.001

%% 1. Sine Wave
f_sine = 5; % Frequency of 5 Hz
sine_wave = sin(2 * pi * f_sine * t);

figure;
subplot(3, 2, 1);
plot(t, sine_wave, 'b', 'LineWidth', 1.5);
grid on;
title('Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');

%% 2. Sawtooth Wave
%%f_sawtooth = 5; % Frequency of 5 Hz
%%sawtooth_wave = sawtooth(2 * pi * f_sawtooth * t);
%% 2. Sawtooth Wave (Manual Implementation)
f_sawtooth = 5; % Frequency of 5 Hz
T_sawtooth = 1 / f_sawtooth; % Period of the sawtooth wave
sawtooth_wave = 2 * (t / T_sawtooth - floor(0.5 + t / T_sawtooth)); % Sawtooth wave formula

subplot(3, 2, 2);
plot(t, sawtooth_wave, 'r', 'LineWidth', 1.5);
grid on;
title('Sawtooth Wave');
xlabel('Time (s)');
ylabel('Amplitude');

%% 3. Step Signal
step_signal = t >= 0; % 1 for t >= 0, 0 otherwise
subplot(3, 2, 3);
plot(t, step_signal, 'g', 'LineWidth', 1.5);
grid on;
title('Step Signal');
xlabel('Time (s)');
ylabel('Amplitude');

%% 4. Ramp Signal
ramp_signal = t .* (t >= 0); % Linear increase for t >= 0

subplot(3, 2, 4);
plot(t, ramp_signal, 'm', 'LineWidth', 1.5);
grid on;
title('Ramp Signal');
xlabel('Time (s)');
ylabel('Amplitude');

%% 5. Pulse Signal
pulse_width = 0.2; % Pulse width of 0.2 seconds
pulse_signal = (t >= -pulse_width/2) & (t <= pulse_width/2); % Pulse is 1 in the range [-0.1, 0.1]

subplot(3, 2, 5);
plot(t, pulse_signal, 'c', 'LineWidth', 1.5);
grid on;
title('Pulse Signal');
xlabel('Time (s)');
ylabel('Amplitude');

%% 6. Impulse Signal
impulse_signal = (t == 0); % Impulse occurs only at t = 0

subplot(3, 2, 6);
stem(t, impulse_signal, 'k', 'LineWidth', 1.5); % Use stem for discrete visualization
grid on;
title('Impulse Signal');
xlabel('Time (s)');
ylabel('Amplitude');
xlim([-0.1 0.1]); % Focus on the impulse

% Adjust layout
sgtitle('Basic Signal Plots');
Code Python:
import numpy as np
import matplotlib.pyplot as plt

# Time vector
t = np.arange(-1, 1, 0.001) # Time from -1 to 1 seconds with a step of 0.001

# 1. Sine Wave
f_sine = 5 # Frequency of 5 Hz
sine_wave = np.sin(2 * np.pi * f_sine * t)

# 2. Sawtooth Wave (Manual Implementation)


f_sawtooth = 5 # Frequency of 5 Hz
T_sawtooth = 1 / f_sawtooth # Period of the sawtooth wave
sawtooth_wave = 2 * (t / T_sawtooth - np.floor(0.5 + t / T_sawtooth)) # Sawtooth formula

# 3. Step Signal
step_signal = np.where(t >= 0, 1, 0) # 1 for t >= 0, 0 otherwise

# 4. Ramp Signal
ramp_signal = np.where(t >= 0, t, 0) # Linear increase for t >= 0

# 5. Pulse Signal
pulse_width = 0.2 # Pulse width of 0.2 seconds
pulse_signal = np.where((t >= -pulse_width / 2) & (t <= pulse_width / 2), 1, 0) # Pulse is 1 in
range [-0.1, 0.1]

# 6. Impulse Signal
impulse_signal = np.where(t == 0, 1, 0) # Impulse occurs only at t = 0

# Plot the signals


plt.figure(figsize=(12, 10))

# Sine Wave
plt.subplot(3, 2, 1)
plt.plot(t, sine_wave, 'b', linewidth=1.5)
plt.grid(True)
plt.title('Sine Wave')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

# Sawtooth Wave
plt.subplot(3, 2, 2)
plt.plot(t, sawtooth_wave, 'r', linewidth=1.5)
plt.grid(True)
plt.title('Sawtooth Wave (Manual)')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

# Step Signal
plt.subplot(3, 2, 3)
plt.plot(t, step_signal, 'g', linewidth=1.5)
plt.grid(True)
plt.title('Step Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

# Ramp Signal
plt.subplot(3, 2, 4)
plt.plot(t, ramp_signal, 'm', linewidth=1.5)
plt.grid(True)
plt.title('Ramp Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

# Pulse Signal
plt.subplot(3, 2, 5)
plt.plot(t, pulse_signal, 'c', linewidth=1.5)
plt.grid(True)
plt.title('Pulse Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

# Impulse Signal
plt.subplot(3, 2, 6)
plt.stem(t, impulse_signal, 'k', basefmt=" ", use_line_collection=True)
plt.grid(True)
plt.title('Impulse Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.xlim(-0.1, 0.1) # Focus on the impulse
# Adjust layout and show
plt.tight_layout()
plt.suptitle('Basic Signal Plots', y=1.02, fontsize=16)
plt.show()
Matlab output
Python output:
Explanation:

General Setup
1. clc;
i. Clears the command window.
ii. Removes all text in the MATLAB console for a clean workspace.
2. clear;
i. Removes all variables, functions, and data from memory.
ii. Resets the workspace.
3. close all;
i. Closes all open figure windows.

Time Vector
4. t = -1:0.001:1; Creates a time vector t ranging from -1 to 1 seconds with a step size of 0.001.

1. Sine Wave
5. f_sine = 5; Sets the frequency of the sine wave to 5 Hz.
6. sine_wave = sin(2 * pi * f_sine * t);
i. Computes the sine wave using the formula y(t)=sin⁡(2πft)y(t) = \sin(2 \pi f t).
ii. 2 * pi * f_sine * t is the angular frequency of the sine wave.
7. Plotting the Sine Wave:
8. figure;
9. subplot(3, 2, 1);
10. plot(t, sine_wave, 'b', 'LineWidth', 1.5);
i. figure;: Creates a new figure window.
ii. subplot(3, 2, 1);: Divides the figure into a grid of 3 rows and 2 columns, and
selects the first subplot for plotting.
iii. plot(t, sine_wave, 'b', 'LineWidth', 1.5);:
▪ Plots the sine wave in blue ('b').
▪ LineWidth adjusts the thickness of the plotted line.
11. Labels and Grid:
12. grid on;
13. title('Sine Wave');
14. xlabel('Time (s)');
15. ylabel('Amplitude');
i. grid on;: Enables grid lines on the plot for better visualization.
ii. title('Sine Wave');: Adds a title to the plot.
iii. xlabel('Time (s)');: Labels the x-axis as "Time (s)".
iv. ylabel('Amplitude');: Labels the y-axis as "Amplitude".

2. Sawtooth Wave
o f_sawtooth = 5; Sets the frequency of the sawtooth wave to 5 Hz.
10. T_sawtooth = 1 / f_sawtooth;
o Calculates the period TT of the sawtooth wave: T=1/frequencyT = 1 /
\text{frequency}.
11. sawtooth_wave = 2 * (t / T_sawtooth - floor(0.5 + t / T_sawtooth));
o Implements the manual formula for a sawtooth wave:
▪ t/Tsawtootht / T_sawtooth: Calculates the phase of the wave within the
period.
▪ floor(0.5 + t / T_sawtooth): Rounds the phase to the nearest integer for
periodicity.
▪ 2⋅(...):2 \cdot (...): Scales the sawtooth wave to have an amplitude of 2
(from -1 to 1).
12. Plotting the Sawtooth Wave:
13. subplot(3, 2, 2);
14. plot(t, sawtooth_wave, 'r', 'LineWidth', 1.5);
15. grid on;
16. title('Sawtooth Wave');
17. xlabel('Time (s)');
18. ylabel('Amplitude');
o Similar to the sine wave but plotted in red ('r').

3. Step Signal
13. step_signal = t >= 0;
o Creates a step signal where:
▪ t >= 0: Returns 1 for all time values t≥0t \geq 0 and 0 otherwise.
14. Plotting the Step Signal:
15. subplot(3, 2, 3);
16. plot(t, step_signal, 'g', 'LineWidth', 1.5);
o Plots the step signal in green ('g').

4. Ramp Signal
15. ramp_signal = t .* (t >= 0);
o Creates a ramp signal where:
▪ t⋅(t≥0)t \cdot (t \geq 0): Multiplies the time vector tt with the condition
t≥0t \geq 0 (1 for t≥0t \geq 0, 0 otherwise).
16. Plotting the Ramp Signal:
17. subplot(3, 2, 4);
18. plot(t, ramp_signal, 'm', 'LineWidth', 1.5);
o Plots the ramp signal in magenta ('m').

5. Pulse Signal
17. pulse_width = 0.2;
o Sets the width of the pulse signal to 0.20.2 seconds.
18. pulse_signal = (t >= -pulse_width/2) & (t <= pulse_width/2);
o Creates a pulse signal where:
▪ t >= -pulse_width/2: Returns 1 for all t≥−0.1t \geq -0.1.
▪ t <= pulse_width/2: Returns 1 for all t≤0.1t \leq 0.1.
▪ Combining these with & creates a pulse of 1 between −0.1-0.1 and 0.10.1,
and 0 elsewhere.
19. Plotting the Pulse Signal:
20. subplot(3, 2, 5);
21. plot(t, pulse_signal, 'c', 'LineWidth', 1.5);
o Plots the pulse signal in cyan ('c').

6. Impulse Signal
20. impulse_signal = (t == 0);
o Creates an impulse signal where:
▪ t == 0: Returns 1 only at t=0t = 0, and 0 elsewhere.
21. Plotting the Impulse Signal:
22. subplot(3, 2, 6);
23. stem(t, impulse_signal, 'k', 'LineWidth', 1.5);
o stem(): Creates a discrete plot (vertical lines with dots) for the impulse.
o Plots the impulse in black ('k').
24. xlim([-0.1 0.1]);
o Sets the x-axis limits to focus on the impulse at t=0t = 0.

Adjust Layout
23. sgtitle('Basic Signal Plots');
o Adds a super-title for all subplots, describing the overall figure.

You might also like