0% found this document useful (0 votes)
15 views8 pages

Day - 1 Explain

The document outlines a MATLAB code that generates and visualizes sine, sawtooth, and triangular signals in both time and frequency domains. It explains the initialization, parameter definitions, signal generation, and plotting processes, including Fourier transforms for frequency analysis. The resulting plots illustrate the shape of the signals over time and their spectral content, highlighting the presence of harmonics in the non-sinusoidal waves.

Uploaded by

khaledsayash
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)
15 views8 pages

Day - 1 Explain

The document outlines a MATLAB code that generates and visualizes sine, sawtooth, and triangular signals in both time and frequency domains. It explains the initialization, parameter definitions, signal generation, and plotting processes, including Fourier transforms for frequency analysis. The resulting plots illustrate the shape of the signals over time and their spectral content, highlighting the presence of harmonics in the non-sinusoidal waves.

Uploaded by

khaledsayash
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/ 8

1.

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:

• xt: Sine wave signal.


• xt_saw: Sawtooth wave signal.
• xt_tri: Triangular wave signal, generated using a sawtooth function with a 0.5 duty cycle (making
it symmetric).

_____________________________________________________
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

% Plotting the Time Domain Signals


figure(1)

subplot(3, 2, 1); % First row, first column


plot(t, xt, 'r')
title('Sine Wave in Time Domain')
xlabel('Time (s)')
ylabel('Amplitude')
grid on

subplot(3, 2, 3); % Second row, first column


plot(t, xt_saw, 'b')
title('Sawtooth Wave in Time Domain')
xlabel('Time (s)')
ylabel('Amplitude')
grid on

subplot(3, 2, 5); % Third row, first column


plot(t, xt_tri, 'g')
title('Triangular Wave in Time Domain')
xlabel('Time (s)')
ylabel('Amplitude')
grid on

% Fourier Transform and Frequency Domain Plotting


n = length(xt); % Number of samples
xf = abs(fftshift(fft(xt, n))) / n; % Fourier transform of sine wave
xf_saw = abs(fftshift(fft(xt_saw, n))) / n; % Fourier transform of sawtooth wave
xf_tri = abs(fftshift(fft(xt_tri, n))) / n; % Fourier transform of triangular wave
df = fs / n; % Frequency resolution
f = -fs/2:df:(fs/2)-df; % Frequency vector

% Plotting the Frequency Domain Signals


subplot(3, 2, 2); % First row, second column
plot(f, xf, 'r');
title('Sine Wave in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
xlim([-100 100])
grid on

subplot(3, 2, 4); % Second row, second column


plot(f, xf_saw, 'b');
title('Sawtooth Wave in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
xlim([-100 100])
grid on

subplot(3, 2, 6); % Third row, second column


plot(f, xf_tri, 'g');
title('Triangular Wave in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
xlim([-100 100])
grid on
Summary
• The code generates and visualizes three types of signals—sine, sawtooth, and
triangular—in both the time and frequency domains.
• The time domain plots show the shape of the signals, while the frequency domain
plots reveal the spectral content, i.e., the distribution of frequencies within each
signal.

- 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.

You might also like