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

SS Matlab Theory

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SS Matlab Theory

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

EC 402 - SIGNALS AND SYSTEM

MATLAB CODE FOR EXPERIMENTS

PLOT ELEMENTRAY SIGNALS USING MATLAB:

% Define the time vector

t = -5:0.01:5;

% Unit Step Function

u = t >= 0;

% Impulse Function

delta = t == 0;

% Ramp Function

r = t .* (t >= 0);

% Plotting the signals

figure;

% Plot Unit Step Function

subplot(3,1,1);

plot(t, u, 'LineWidth', 2);

title('Unit Step Function');

xlabel('Time (t)');

ylabel('u(t)');

grid on;

% Plot Impulse Function

subplot(3,1,2);

stem(t, delta, 'LineWidth', 2);

title('Impulse Function');
xlabel('Time (t)');

ylabel('\delta(t)');

grid on;

% Plot Ramp Function

subplot(3,1,3);

plot(t, r, 'LineWidth', 2);

title('Ramp Function');

xlabel('Time (t)');

ylabel('r(t)');

grid on;

2. PLOT REAL AND COMPLEX EXPONENTIAL SIGNALS, RECTANGULAR AND


TRIANGULAR PULSE FUNCTION, SIGNUM, SINE AND COSINE GAUSSIAN FUNCTION
USING MATLAB:

% Define the time vector

t = -5:0.01:5;

% Real Exponential Signal

alpha = 0.5;

real_exp = exp(alpha * t);

% Complex Exponential Signal

omega = pi; % Frequency

complex_exp = exp(1j * omega * t);

% Rectangular Signal

rect_signal = @(t) (abs(t) <= 0.5); % Rectangular function

% Triangular Signal

tri_signal = @(t) (1 - abs(t)) .* (abs(t) <= 1); % Triangular function

% Signum Function
signum = @(t) sign(t);

% Define the frequency for sine and cosine

f = 1; % Frequency in Hz

% Sine and Cosine Signals

sine_signal = sin(2 * pi * f * t);

cosine_signal = cos(2 * pi * f * t);

% Plotting the signals

figure;

% Real Exponential Signal

subplot(3,3,1);

plot(t, real_exp, 'LineWidth', 2);

title('Real Exponential Signal');

xlabel('Time (t)');

ylabel('exp(\alpha t)');

grid on;

% Complex Exponential Signal - Real Part

subplot(3,3,2);

plot(t, real(complex_exp), 'LineWidth', 2);

title('Complex Exponential Signal - Real Part');

xlabel('Time (t)');

ylabel('Re\{exp(j\omega t)\}');

grid on;

% Complex Exponential Signal - Imaginary Part

subplot(3,3,3);

plot(t, imag(complex_exp), 'LineWidth', 2);

title('Complex Exponential Signal - Imaginary Part');


xlabel('Time (t)');

ylabel('Im\{exp(j\omega t)\}');

grid on;

% Rectangular Signal

subplot(3,3,4);

plot(t, rect_signal(t), 'LineWidth', 2);

title('Rectangular Signal');

xlabel('Time (t)');

ylabel('rect(t)');

grid on;

% Triangular Signal

subplot(3,3,5);

plot(t, tri_signal(t), 'LineWidth', 2);

title('Triangular Signal');

xlabel('Time (t)');

ylabel('tri(t)');

grid on;

% Signum Function

subplot(3,3,6);

plot(t, signum(t), 'LineWidth', 2);

title('Signum Function');

xlabel('Time (t)');

ylabel('sign(t)');

grid on;

% Sine Function

subplot(3,3,7);
plot(t, sine_signal, 'LineWidth', 2);

title('Sine Function');

xlabel('Time (t)');

ylabel('sin(2\pi ft)');

grid on;

% Cosine Function

subplot(3,3,8);

plot(t, cosine_signal, 'LineWidth', 2);

title('Cosine Function');

xlabel('Time (t)');

ylabel('cos(2\pi ft)');

grid on;

3.PLOT SAME SIGNALS IN DISCRETE TIME IN MATLAB:

% Define the discrete time vector

n = -50:50;

% Real Exponential Signal (DT)

alpha = 0.1;

real_exp_dt = exp(alpha * n);

% Complex Exponential Signal (DT)

omega = pi / 10; % Frequency

complex_exp_dt = exp(1j * omega * n);

% Rectangular Signal (DT)

rect_signal_dt = @(n) (abs(n) <= 5); % Rectangular function

% Triangular Signal (DT)

tri_signal_dt = @(n) (1 - abs(n)/5) .* (abs(n) <= 5); % Triangular function


% Signum Function (DT)

signum_dt = @(n) sign(n);

% Define the frequency for sine and cosine

f = 0.05; % Frequency in Hz

% Sine and Cosine Signals (DT)

sine_signal_dt = sin(2 * pi * f * n);

cosine_signal_dt = cos(2 * pi * f * n);

% Plotting the signals

figure;

% Real Exponential Signal (DT)

subplot(3,3,1);

stem(n, real_exp_dt, 'LineWidth', 2);

title('Real Exponential Signal (DT)');

xlabel('Time index n');

ylabel('exp(\alpha n)');

grid on;

% Complex Exponential Signal - Real Part (DT)

subplot(3,3,2);

stem(n, real(complex_exp_dt), 'LineWidth', 2);

title('Complex Exponential Signal - Real Part (DT)');

xlabel('Time index n');

ylabel('Re\{exp(j\omega n)\}');

grid on;

% Complex Exponential Signal - Imaginary Part (DT)

subplot(3,3,3);

stem(n, imag(complex_exp_dt), 'LineWidth', 2);


title('Complex Exponential Signal - Imaginary Part (DT)');

xlabel('Time index n');

ylabel('Im\{exp(j\omega n)\}');

grid on;

%Rectangular Signal (DT)

subplot(3,3,4);

stem(n, rect_signal_dt(n), 'LineWidth', 2);

title('Rectangular Signal (DT)');

xlabel('Time index n');

ylabel('rect[n]');

grid on;

% Triangular Signal (DT)

subplot(3,3,5);

stem(n, tri_signal_dt(n), 'LineWidth', 2);

title('Triangular Signal (DT)');

xlabel('Time index n');

ylabel('tri[n]');

grid on;

% Signum Function (DT)

subplot(3,3,6);

stem(n, signum_dt(n), 'LineWidth', 2);

title('Signum Function (DT)');

xlabel('Time index n');

ylabel('sign[n]');

grid on;

% Sine Function (DT)


subplot(3,3,7);

stem(n, sine_signal_dt, 'LineWidth', 2);

title('Sine Function (DT)');

xlabel('Time index n');

ylabel('sin(2\pi fn)');

grid on;

% Cosine Function (DT)

subplot(3,3,8);

stem(n, cosine_signal_dt, 'LineWidth', 2);

title('Cosine Function (DT)');

xlabel('Time index n');

ylabel('cos(2\pi fn)');

grid on;

4. INTRODUCTION TO SIMULINK:

Simulink is a graphical programming environment integrated with MATLAB, designed for


modeling, simulating, and analyzing multidomain dynamical systems. It offers an
interactive, block diagram-based interface for constructing and running simulations of
systes ranging from simple dynamic models to complex control systems and signal
processing applications.

Key Features of Simulink

1. Graphical Interface:

- Simulink provides a block diagram interface where users can drag and drop blocks
representing various components of a system. These blocks can be connected to define
the flow of signals and data between them.

2. Predefined Block Libraries:


- Simulink comes with extensive libraries of predefined blocks for various domains, such
as continuous and discrete dynamics, math operations, signal routing, logic operations,
and more. These blocks can be customized to suit specific modeling needs.

3. Modeling:

- Simulink supports modeling systems that span multiple domains, such as mechanical,
electrical, hydraulic, and thermal systems, allowing for the integration of different physical
components into a single simulation environment.

4. Simulation and Analysis

- Simulink provides powerful simulation capabilities, enabling users to run their models
and observe the behavior of the system over time. Results can be visualized using scopes,
plots, and other visualization tools.

5. Integration with MATLAB:

- Simulink is tightly integrated with MATLAB, allowing users to leverage MATLAB's


computational capabilities and script-based environment for additional data processing,
control algorithm design, and more.

6. Code Generation:

- Simulink offers automatic code generation tools that can convert models into C, C++, or
HDL code. This feature is particularly useful for implementing control algorithms on
embedded systems and hardware platforms.

7. Model-Based Design:

- Simulink supports a model-based design approach, which allows engineers to design,


simulate, and verify systems before implementing them in real hardware. This approach
helps in identifying and fixing issues early in the design process, reducing development
time and costs.
Basic Workflow in Simulink

1. Creating a Model:

- Start by opening Simulink from the MATLAB interface and creating a new model. Add
blocks to the model from the library browser and connect them to define the system.

2. Configuring Parameters:

- Set parameters for each block according to the system requirements. Parameters can
include gains, initial conditions, sample times, etc.

3. Running Simulations:

- Configure the simulation settings, such as start and stop times, solver options, and step
sizes. Run the simulation to see how the system behaves over time.

4. Analyzing Results:

- Use scopes and other visualization blocks to observe the outputs of the system. Analyze
the results to ensure the system meets the desired performance criteria.

5. Refining the Model:

- Modify the model as needed based on the simulation results. This iterative process
helps in refining the design and improving system performance.

6. Generating Code (if needed):

- Use the code generation tools to convert the model into executable code for
deployment on hardware platforms.

Example: Simple Simulink Model


Let's create a simple Simulink model to simulate a first-order linear system:

1. Open Simulink and create a new model.

2. From the Simulink Library Browser, add the following blocks to the model:

- Step block (under Sources)

- Transfer Function block (under Continuous)

- Scope block (under Sinks)

3. Connect the blocks as follows:

- Connect the output of the Step block to the input of the Transfer Function block.

- Connect the output of the Transfer Function block to the input of the Scope block.

4. Configure the Transfer Function block:

- Double-click the Transfer Function block to open its parameters window.

- Set the numerator to `[1]` and the denominator to `[1 1]`, representing a first-order
system with a time constant of 1.

5. Configure the Step block:

- Double-click the Step block to open its parameters window.

- Set the Step time to `1`, Initial value to `0`, and Final value to `1`.

6. Run the simulation:

- Click the Run button on the Simulink toolbar.

- Double-click the Scope block to view the output response of the system.
This simple example demonstrates how to create and simulate a basic dynamic system in
Simulink. As you become more familiar with Simulink, you can explore its advanced
features and build more complex models.

Simulink's intuitive interface and powerful simulation capabilities make it a valuable tool
for engineers and researchers working on a wide range of applications, from control
systems and signal processing to mechanical and electrical system design.

5.ANALYSE THE BASIC SIGNAL USING SIMULINK BLOCK:

Analyzing basic signals using Simulink involves creating models that generate, process,
and visualize these signals. Here's a step-by-step guide on how to analyze basic signals,
including sine, cosine, step, impulse, and ramp functions, using Simulink.

Step 1: Open Simulink

1. Open MATLAB.

2. In the MATLAB command window, type `simulink` and press Enter to open the Simulink
library browser.

Step 2: Create a New Model

1. In the Simulink start page, click on "Blank Model" to create a new model.

### Step 3: Add Source Blocks

To generate the basic signals, we need to add source blocks from the Simulink library.

1. **Sine Wave**:
- In the Simulink Library Browser, navigate to `Sources`.

- Drag and drop the **Sine Wave** block into the model.

2. **Cosine Wave**:

- Simulink doesn't have a direct Cosine Wave block, but you can use a Sine Wave block
and adjust the phase.

- Drag and drop another **Sine Wave** block into the model.

- Double-click the block and set the Phase (rad) parameter to `pi/2` to generate a cosine
wave.

3. **Step Function**:

- Navigate to `Sources`.

- Drag and drop the **Step** block into the model.

4. **Impulse Function**:

- Navigate to `Sources`.

- Drag and drop the **Pulse Generator** block into the model.

- Set the Pulse type to "Sample based", set the Amplitude to `1`, Period to a large value
(e.g., `1000`), and Pulse width to a very small value (e.g., `1`).

5. **Ramp Function**:

- Navigate to `Sources`.

- Drag and drop the **Ramp** block into the model.

Step 4: Add Scope Blocks

To visualize the signals, we need to add Scope blocks.


1. Navigate to `Sinks`.

2. Drag and drop the **Scope** block into the model.

3. Create multiple Scope blocks for each signal or use a **Mux** block to combine signals
into one Scope.

Step 5: Connect the Blocks

1. Connect each signal source block to its corresponding Scope block.

2. If using a Mux block, connect the outputs of the signal source blocks to the inputs of the
Mux block, and connect the output of the Mux block to the Scope block.

Step 6: Configure the Blocks

1. **Sine Wave** and **Cosine Wave**:

- Double-click the Sine Wave blocks and set the parameters such as Amplitude,
Frequency (rad/sec), and Phase (for cosine).

2. **Step**:

- Double-click the Step block and set the Step time, Initial value, and Final value.

3. **Impulse**:

- Double-click the Pulse Generator block and configure it as mentioned above.

4. **Ramp**:

- Double-click the Ramp block and set the Slope and Start time.

Step 7: Run the Simulation


1. Click the **Run** button on the Simulink toolbar.

2. Double-click the Scope blocks to view the signal waveforms.

Example: Complete Model Setup

Here's how the Simulink model setup might look in text form:

1. Open Simulink and create a blank model.

2. Add the following blocks from the library:

- **Sources**: Sine Wave, Step, Pulse Generator, Ramp.

- **Sinks**: Scope.

- **Signal Routing**: Mux (optional, if using a single Scope).

3. Configure the blocks:

- Sine Wave: Amplitude = 1, Frequency = 1 rad/sec.

- Cosine Wave: Amplitude = 1, Frequency = 1 rad/sec, Phase = pi/2.

- Step: Step time = 1, Initial value = 0, Final value = 1.

- Pulse Generator (Impulse): Amplitude = 1, Period = 1000, Pulse width = 1.

- Ramp: Slope = 1, Start time = 0.

4. Connect the blocks:

- Sine Wave to Scope.

- Cosine Wave to Scope.

- Step to Scope.

- Pulse Generator to Scope.


- Ramp to Scope.

Detailed Steps for Each Signal in Simulink

Sine and Cosine Wave

1. **Sine Wave**: Add and configure the Sine Wave block.

- Set Amplitude = 1, Frequency = 2*pi (for 1 Hz), Phase = 0.

2. **Cosine Wave**: Add and configure another Sine Wave block.

- Set Amplitude = 1, Frequency = 2*pi (for 1 Hz), Phase = pi/2 (90 degrees).

3. Connect both to individual Scope blocks or a Mux connected to a single Scope.

Step Function

1. Add the Step block.

- Set Step time = 1, Initial value = 0, Final value = 1.

2. Connect to a Scope block.

Impulse Function

1. Add the Pulse Generator block.

- Set Amplitude = 1, Period = 1000, Pulse width = 1 (very narrow pulse).

2. Connect to a Scope block.

Ramp Function
1. Add the Ramp block.

- Set Slope = 1, Start time = 0.

2. Connect to a Scope block.

### Running the Simulation

1. Click the **Run** button on the Simulink toolbar.

2. Open the Scope blocks to view the generated signals.

By following these steps, you can create and analyze various basic signals in Simulink,
allowing for a visual understanding of their behaviors and properties.

6. MULTIPLICATION OF TWO CONTINOUS TIME SIGNAL:

% Define the time vector

t = -2*pi:0.01:2*pi;

% Define the continuous-time signals

A1 = 1; % Amplitude of first signal

f1 = 1; % Frequency of first signal (Hz)

signal1 = A1 * sin(2*pi*f1*t); % First signal (sine wave)

A2 = 1; % Amplitude of second signal

f2 = 1; % Frequency of second signal (Hz)

signal2 = A2 * cos(2*pi*f2*t); % Second signal (cosine wave)

% Multiply the two signals

product_signal = signal1 .* signal2;

% Plot the signals

figure;
% Plot the first signal (sine wave)

subplot(3,1,1);

plot(t, signal1, 'LineWidth', 2);

title('Signal 1: Sine Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

% Plot the second signal (cosine wave)

subplot(3,1,2);

plot(t, signal2, 'LineWidth', 2);

title('Signal 2: Cosine Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

% Plot the product of the two signals

subplot(3,1,3);

plot(t, product_signal, 'LineWidth', 2);

title('Product of Sine and Cosine Waves');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

7.GENERATION OF ELEMENTARY SIGNAL SEQUENCES IN DISCRETE TIME:

% Define the discrete time vector

n = -10:10;
%% Impulse Signal (Unit Impulse)

impulse_signal = (n == 0);

%% Step Signal (Unit Step)

step_signal = (n >= 0);

%% Ramp Signal

ramp_signal = (n >= 0) .* n;

%% Exponential Signal

alpha = 0.8; % Decay rate

exponential_signal = alpha.^n .* (n >= 0);

%% Sine Signal

f = 0.1; % Frequency

sine_signal = sin(2*pi*f*n);

%% Cosine Signal

cosine_signal = cos(2*pi*f*n);

%% Plotting the Signals

figure;

% Impulse Signal

subplot(3,2,1);

stem(n, impulse_signal, 'LineWidth', 2);

title('Impulse Signal (Unit Impulse)');

xlabel('n');

ylabel('Amplitude');

grid on;

% Step Signal

subplot(3,2,2);

stem(n, step_signal, 'LineWidth', 2);


title('Step Signal (Unit Step)');

xlabel('n');

ylabel('Amplitude');

grid on;

% Ramp Signal

subplot(3,2,3);

stem(n, ramp_signal, 'LineWidth', 2);

title('Ramp Signal');

xlabel('n');

ylabel('Amplitude');

grid on;

% Exponential Signal

subplot(3,2,4);

stem(n, exponential_signal, 'LineWidth', 2);

title('Exponential Signal');

xlabel('n');

ylabel('Amplitude');

grid on;

% Sine Signal

subplot(3,2,5);

stem(n, sine_signal, 'LineWidth', 2);

title('Sine Signal');

xlabel('n');

ylabel('Amplitude');

grid on;

% Cosine Signal
subplot(3,2,6);

stem(n, cosine_signal, 'LineWidth', 2);

title('Cosine Signal');

xlabel('n');

ylabel('Amplitude');

grid on;

8. TESTING ORTHOGONALITY BETWEEN TWO SIGNALS:

Orthogonality between two signals can be tested using the inner product of the signals. If
the inner product of two signals is zero, the signals are orthogonal. For discrete-time
signals x[n]x[n]x[n] and y[n]y[n]y[n], the inner product is defined as:

⟨x,y⟩=∑nx[n]⋅y[n]\langle x, y \rangle = \sum_{n} x[n] \cdot y[n]⟨x,y⟩=∑n x[n]⋅y[n]

If ⟨x,y⟩=0\langle x, y \rangle = 0⟨x,y⟩=0, the signals are orthogonal.

% Define the discrete time vector

n = 0:10;

% Define two signals (you can modify these signals)

signal1 = cos(2 * pi * 0.1 * n); % Example signal 1: cosine wave

signal2 = sin(2 * pi * 0.1 * n); % Example signal 2: sine wave

% Calculate the inner product of the two signals

inner_product = sum(signal1 .* signal2);

% Display the result

fprintf('Inner product of the two signals: %f\n', inner_product);

% Check for orthogonality

if abs(inner_product) < 1e-10


fprintf('The signals are orthogonal.\n');else

fprintf('The signals are not orthogonal.\n');

end

% Plot the signals for visualizati figurE:

subplot(2,1,1);

stem(n, signal1, 'LineWidth', 2);

title('Signal 1: Cosine Wave');

xlabel('n');

ylabel('Amplitude');

grid on;

subplot(2,1,2);

stem(n, signal2, 'LineWidth', 2);

title('Signal 2: Sine Wave');

xlabel('n');

ylabel('Amplitude');

grid on;

9.FINDING EXPONENTIAL FORIER SERIES OF VARIOUS PRIODIC SIGNALS:

% Define the fundamental period T and fundamental frequency w0

T = 2*pi; % Fundamental period

w0 = 2*pi/T; % Fundamental frequency % Define the number of harmonics to compute

N = 20;

% Time vector for one period


t = linspace(-T/2, T/2, 1000);

% Define the periodic signals over one period

% 1. Square wave

square_wave = square(w0 * t);

% 2. Sawtooth wave

sawtooth_wave = sawtooth(w0 * t);

% 3. Triangular wave

triangular_wave = sawtooth(w0 * t, 0.5);

% Function to compute the EFS coefficienT

function Ck = compute_efs_coefficients(signal, t, T, N)

w0 = 2 * pi / T;Ck = zeros(1, 2*N+1);

or k = -N:N

k(kN+1) = (1/T) * trapz(t, signal .* exp(-1j * k * w0 * t));

end

% Compute EFS coefficients for each signal

Ck_square = compute_efs_coefficients(square_wave, t, T, N);

Ck_sawtooth = compute_efs_coefficients(sawtooth_wave, t, T, N);

Ck_triangular = compute_efs_coefficients(triangular_wave, t, T, N);

% Reconstruct the signals using the EFS coefficients (optional)

reconstructed_square = zeros(size(t));

reconstructed_sawtooth = zeros(size(t));

reconstructed_triangular = zeros(size(t));
for k = -N:N

reconstructed_square = reconstructed_square + Ck_square(k+N+1) * exp(1j * k * w0 * t);

reconstructed_sawtooth = reconstructed_sawtooth + Ck_sawtooth(k+N+1) * exp(1j * k * w


0 * t);triangular = reconstructed_triangular + Ck_triangular(k+N+1) * exp(1j * k * w0 * t);

end

% Plot the original and reconstructed signals

figure;

subplot(3,2,1);

plot(t, square_wave);

title('Original Square Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(3,2,2);

plot(t, real(reconstructed_square));

title('Reconstructed Square Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(3,2,3);

plot(t, sawtooth_wave);

title('Original Sawtooth Wave');

xlabel('Time (t)');
ylabel('Amplitude');

grid on;

subplot(3,2,4);

plot(t, real(reconstructed_sawtooth));

title('Reconstructed Sawtooth Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(3,2,5);

plot(t, triangular_wave);

title('Original Triangular Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(3,2,6);

plot(t, real(reconstructed_triangular));

title('Reconstructed Triangular Wave');

xlabel('Time (t)');

ylabl('Amplitude');

grid on;

10.EXPONENTIAL FOURIER SERIES OF A PERIODIC FULL WAVE RECTIFIER:


% Define parameters

T = pi; % Fundamental period

w0 = 2*pi/T; % Fundamental frequency

N = 10; % Number of harmonics

% Time vector for one period

t = linspace(-T/2, T/2, 1000);

% Define the full-wave rectified sine wave over one period

x_t = (2/pi) * (sin(w0 * t) + (1/3) * sin(3*w0 * t) + (1/5) * sin(5*w0 * t));

$Function to compute the EFS coefficients

function Ck = compute_efs_coefficients(signal, t, T, N)

w0 = 2 * pi / T;

Ck = zeros(1, 2*N+1);

for k = -N:N

Ck(k+N+1) = (1/T) * trapz(t, signal .* exp(-1j * k * w0 * t));

end

end

% Compute EFS coefficients

Ck = compute_efs_coefficients(x_t, t, T, N);

% Reconstruct the signal using EFS coefficients (optional)

reconstructed_x_t = zeros(size(t));

for k = -N:N

reconstructed_x_t = reconstructed_x_t + Ck(k+N+1) * exp(1j * k * w0 * t);


end

% Plot the original and reconstructed signals

figure;

subplot(2,1,1);

plot(t, x_t, 'LineWidth', 2);

title('Original Full-Wave Rectified Sine Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(2,1,2);

plot(t, real(reconstructed_x_t), 'LineWidth', 2);

title('Reconstructed Full-Wave Rectified Sine Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

% Display EFS coefficients

disp('Exponential Fourier Series Coefficients:');

disp(Ck);

11. TO GENERATE TRIGNOMETRIC FOURIER SERIES OF PERIODIC RECTANGULAR


SIGNAL:

% Define parameters

T = 2*pi; % Fundamental period

w0 = 2*pi/T; % Fundamental frequency


A = 1; % Amplitude of the rectangular wave

N = 10; % Number of harmonics

% Time vector for one period

t = linspace(0, T, 1000);

% Define the rectangular wave over one period

x_t = A * rectpuls(mod(t, T) - T/2, T);

% Function to compute the TFS coefficients

a0 = (1/T) * trapz(t, x_t);

an = zeros(1, N);

bn = zeros(1, N);

for n = 1:N

an(n) = (2/T) * trapz(t, x_t .* cos(n * w0 * t));bn(n) = (2/T) * trapz(t, x_t .* sin(n * w0 * t));

end

% Reconstruct the signal using TFS coefficients

reconstructed_x_t = a0/2;

for n = 1:N

reconstructed_x_t = reconstructed_x_t + an(n) * cos(n * w0 * t) + bn(n) * sin(n * w0 * t);

end

% Plot the original and reconstructed signals

figure;

subplot(2,1,1);

plot(t, x_t, 'LineWidth', 2);


title('Original Rectangular Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(2,1,2);

plot (t, reconstructed_x_t, 'LineWidth', 2);

title('Reconstructed Rectangular Wave (TFS)');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

12.FOURIER TRANSFORM AND INVERSE FOURIER TRANSFORM OF SEQUENCE:

% Define time vector

t = linspace(-5, 5, 1000); % Time vector from -5 to 5

% Example signal (continuous)

x_t = exp(-t.^2); % Gaussian function as an example

% Compute the Fourier Transform using fft function

X_w = fftshift(fft(x_t));

% Compute the Inverse Fourier Transform using ifft function

x_t_reconstructed = ifft(ifftshift(X_w));

% Plotting the results

figure;

subplot(2, 1, 1);
plot(t, x_t, 'LineWidth', 2);

title('Original Signal (Gaussian)');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

subplot(2, 1, 2);

plot(t, abs(x_t_reconstructed), 'LineWidth', 2);

title('Reconstructed Signal (IFT of FT)');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;

% Display results

disp('Original Signal (Gaussian):');

disp(x_t);

disp('FT (Shifted):');

disp(X_w);

disp('Reconstructed Signal:');

disp(x_t_reconstructed);

13.TIME SCALING PROPERTIES OF FOURIER TRANSFORM OF SINE SIGNAL:

% Parameters

omega0 = 2*pi; % Angular frequency of original sine wave

a = 2; % Scaling factor
% Time vector

t = linspace(-10, 10, 1000);

% Original sine wave

x_t = sin(omega0 * t);

% Scaled sine wave

x_at = sin(omega0 * a * t);

% Fourier Transform of original sine wave

X_omega = fftshift(fft(x_t));

% Fourier Transform of scaled sine wave

X_a_omega = fftshift(fft(x_at));

% Frequency vector

N = length(t);

fs = 1/(t(2)-t(1)); % Sampling frequency

f = linspace(-fs/2, fs/2, N);

% Plotting

figure;

subplot(2, 1, 1);

plot(t, x_t, 'LineWidth', 2);

title('Original Sine Wave');

xlabel('Time (t)');

ylabel('Amplitude');

grid on;
subplot(2, 1, 2);

plot(t, x_at, 'LineWidth', 2);

title('Scaled Sine Wave (a = 2)');

xlabel('Time (at)');

ylabel('Amplitude');

grid on;

figure;

subplot(2, 1, 1);

plot(f, abs(X_omega), 'LineWidth', 2);

title('Fourier Transform of Original Sine Wave');

xlabel('Frequency (\omega)');

ylabel('Magnitude');

grid on;

xlim([-3*omega0, 3*omega0]);

subplot(2, 1, 2);

plot(f, abs(X_a_omega) / abs(a), 'LineWidth', 2); % Scaling by |a|

title('Scaled Fourier Transform (a = 2)');

xlabel('Frequency (\omega)');

ylabel('Magnitude');

grid on;

xlim([-3*omega0, 3*omega0]);

14.CONVOLUTION USING FOURIER TRANSFORM SYMBOLIC:


% Clear previous symbolic variables

clear;

%Define symbolic variable t

syms t omega;

% Define symbolic functions f(t) and g(t)

f_t = heaviside(t); % Unit step function

g_t = sin(t); % Sinusoidal function

% Compute Fourier Transforms F(omega) and G(omega)

F_omega = fourier(f_t, t, omega);

G_omega = fourier(g_t, t, omega);

% Compute H(omega) = F(omega) * G(omega)

H_omega = simplify(F_omega * G_omega);

% Compute inverse Fourier Transform to get convolution h(t) = f(t) * g(t)

h_t = simplify(ifourier(H_omega, omega, t));

% Display the results

disp('Symbolic Convolution Using Fourier Transform:');

disp('-------------------------------------------');

disp('f(t):');

disp(f_t);

disp('g(t):');

disp(g_t);
disp('h(t) = f(t) * g(t):');

disp(h_t);

You might also like