Day - 1 Explain
Day - 1 Explain
Initialization
clc; K.A
clear;
close all;
Explanation:
• clc: Clears the command window.
• clear: Removes all variables from the workspace.
• close all: Closes all open figure windows.
_____________________________________________________
2. Define Parameters
A = 3;
f0 = 10; % Frequency for one cycle (10 Hz)
T0 = 1/f0; % Time period for one cycle
cycle = 10;
T = cycle * T0; % Total time for all cycles
fs = 200 * f0; % Sampling frequency (200 times the signal frequency)
dt = 1/fs; % Time step between samples
t = 0:dt:T-dt; % Time vector from 0 to T-dt
Explanation:
• A: Amplitude of the signals.
• f0: Fundamental frequency of the signals (10 Hz).
• T0: Time period for one cycle of the signals.
• cycle: Number of cycles of the signal to generate.
• T: Total duration of the signal, accounting for all cycles.
• fs: Sampling frequency, which is 200 times the signal frequency to ensure accurate signal
representation.
• dt: Time interval between each sample.
• t: Time vector, representing discrete time points at which the signals are sampled.
3. Generate Signals
xt = A * sin(2 * pi * f0 * t);
xt_saw = A * sawtooth(2 * pi f0 * t);
xt_tri = A * sawtooth(2 * pi * f0 * t, 0.5); % Triangular wave
Explanation:
_____________________________________________________
4. Plot Time Domain Signals Explanation:
figure(1) - figure(1): Creates a new figure window.
subplot(3,2,1); - subplot(m,n,p): Divides the figure into an m x n
plot(t, xt, 'r') grid, and p specifies the location for the current plot.
title('Sin Wave at Time Domain') - plot(): Plots the signals in the time domain.
xlabel('Time') - xlabel, ylabel, title: Labels the axes and titles
ylabel('x(t)') the plots.
grid on - grid on: Adds a grid to the plot for better
subplot(3,2,3); visualization.
plot(t, xt_saw, 'b')
title('Sawtooth Wave at Time Domain')
xlabel('Time')
ylabel('x(t)')
grid on
subplot(3,2,5);
plot(t, xt_tri, 'g')
title('Triangular Wave at Time Domain')
xlabel('Time')
ylabel('x(t)')
grid on
5. Compute Fourier Transforms
n = length(xt);
xf = abs(fftshift(fft(xt,n)))/n;
xf_saw = abs(fftshift(fft(xt_saw,n)))/n;
xf_tri = abs(fftshift(fft(xt_tri,n)))/n;
df = fs/n;
f = -fs/2:df:(fs/2)-df;
Explanation:
• n: Number of samples in the signals.
• fft(): Computes the Fast Fourier Transform of the signals.
• fftshift(): Shifts the zero frequency component to the center of the spectrum.
• abs(): Computes the magnitude of the complex Fourier coefficients.
• xf, xf_saw, xf_tri: Magnitudes of the Fourier transforms of the sine, sawtooth, and
triangular waves, respectively.
• df: Frequency resolution.
• f: Frequency vector, which defines the range of frequencies from -fs/2 to fs/2 - df.
6. Plot Frequency Domain Signals
subplot(3,2,2);
plot(f, xf, 'r');
title('Sin Wave at Frequency Domain')
xlabel('Frequency')
ylabel('x(f)')
xlim([-fs fs])
grid on
subplot(3,2,4);
plot(f, xf_saw, 'b');
title('Sawtooth Wave at Frequency Domain')
xlabel('Frequency')
ylabel('x(f)')
xlim([-100 100])
grid on
subplot(3,2,6);
plot(f, xf_tri, 'r');
title('Triangular Wave at Frequency Domain')
xlabel('Frequency')
ylabel('x(f)')
xlim([-100 100])
grid on
Explanation:
• Plots the frequency spectra of the three signals in the frequency domain.
• xlim([-fs fs]): Limits the x-axis (frequency axis) to the range from -fs to fs.
• xlim([-100 100]): Restricts the frequency range to visualize the fundamental
frequency and a few harmonics more clearly for the sawtooth and triangular waves.
Source Code
clc; % Clear command window
clear; % Clear workspace variables
close all; % Close all figure windows
% Parameters
A = 3; % Amplitude
f0 = 10; % Frequency for one cycle (10 Hz)
T0 = 1/f0; % Time period for one cycle
cycle = 10; % Number of cycles
T = cycle * T0; % Total time for all cycles
fs = 200 * f0; % Sampling frequency (200 times the signal frequency)
dt = 1/fs; % Time step between samples
t = 0:dt:T-dt; % Time vector from 0 to T-dt
% Generate Signals
xt = A * sin(2 * pi * f0 * t); % Sine wave
xt_saw = A * sawtooth(2 * pi * f0 * t); % Sawtooth wave
xt_tri = A * sawtooth(2 * pi * f0 * t, 0.5); % Triangular wave
- Time Domain:
• The top left plot shows the sine wave over time.
• The middle left plot displays the sawtooth wave over time.
• The bottom left plot shows the triangular wave over time.
- Frequency Domain:
• The top right plot shows the frequency components of the sine wave.
• The middle right plot displays the frequency spectrum of the sawtooth wave, which includes harmonics due to its non-sinusoidal
nature.
• The bottom right plot shows the frequency spectrum of the triangular wave, which also includes harmonics, but with different
amplitudes compared to the sawtooth wave.