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

MATLAB Code (2)

The document contains four separate codes related to signal processing. Code 1 demonstrates amplitude modulation, Code 2 illustrates periodic and aperiodic sequences using sine waves, Code 3 computes the discrete Fourier transform (DFT) of a time-domain signal, and Code 4 performs the inverse discrete Fourier transform (IDFT) to retrieve the time-domain representation from the frequency domain. Each code includes visualizations of the signals and their respective transformations.

Uploaded by

ali974424
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)
1 views

MATLAB Code (2)

The document contains four separate codes related to signal processing. Code 1 demonstrates amplitude modulation, Code 2 illustrates periodic and aperiodic sequences using sine waves, Code 3 computes the discrete Fourier transform (DFT) of a time-domain signal, and Code 4 performs the inverse discrete Fourier transform (IDFT) to retrieve the time-domain representation from the frequency domain. Each code includes visualizations of the signals and their respective transformations.

Uploaded by

ali974424
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

CODE 1.(AM-Modulation).

clc; % Clear command window


clear;
close all; % Close all figure windows

% Parameters
m = 1; % Modulation index or Modulation Factor or Modulation depth
Am = 5; % Amplitude of modulating signal
fm = 2000; % Frequency of modulating signal (Hz)
Tm = 1 / fm; % Period of modulating signal (s)
t = 0:Tm/999:6*Tm; % Time vector for 6 periods of the modulating
signal
Ac = Am / m; % Amplitude of carrier signal
fc = fm * 10; % Frequency of carrier signal (10 times the modulating
signal frequency)
Tc = 1 / fc; % Period of carrier signal (s)

% Modulating Signal
Ym = Am * sin(2 * pi * fm * t); % Modulating (message) signal
subplot(3,1,1); % Create the first subplot
plot(t, Ym, 'b', 'LineWidth', 0.8); % Blue line with thicker width for
better visibility
title('Modulating Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Carrier Signal

Yc = Ac * sin(2 * pi * fc * t); % Carrier signal


subplot(3,1,2); % Create the second subplot
plot(t, Yc, 'r', 'LineWidth', 0.8); % Red line with thicker width
title('Carrier Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Amplitude Modulated (AM) Signal
AMsig = Ac * (1 + m * sin(2 * pi * fm * t)) .* sin(2 * pi * fc * t); % AM
signal
subplot(3,1,3); % Create the third subplot
plot(t, AMsig, 'g', 'LineWidth', 0.8); % Green line with thicker width
title('Amplitude Modulated Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
CODE 2.(PERIODIC & APERIODIC SEQUENCES).
% Define the sample points from 0 to 50
n = 0:1:50;

% Generate the first sine wave with frequency 0.2*pi


x1 = sin(0.2*pi*n);

% Generate the second sine wave with frequency 0.4*pi


x2 = sin(0.4*pi*n);

% Create a signal that is the superposition of the two previous


sine waves
x3 = sin(0.2*pi*n) + sin(0.4*pi*n);

% Generate a fourth sine wave with frequency 0.5


x4 = sin(0.5*n);

% Create a 2x2 subplot layout

% First subplot - Display the first sine wave


subplot(2, 2, 1);
stem(n, x1);
xlabel('n');
ylabel('x1(n)');
axis([0 50 -1 1]); % Set axis limits

% Second subplot - Display the second sine wave


subplot(2, 2, 2);
stem(n, x2);
xlabel('n');
ylabel('x2(n)');
axis([0 50 -1 1]); % Set axis limits

% Third subplot - Display the superposition of the two sine waves


subplot(2, 2, 3);
stem(n, x3);
xlabel('n');
ylabel('x3(n)');
axis([0 50 -2 2]); % Set axis limits (note the larger y-range to
accommodate the superposition)

% Fourth subplot - Display the fourth sine wave


% Note: Fixed variable name from 'n1' to 'n' to match the defined
variable
subplot(2, 2, 4);
stem(n, x4); % Changed n1 to n
xlabel('n');
ylabel('x4(n)');
axis([0 50 -1 1]); % Set axis limits
CODE 3.(DISCRETE FOURIER TRANSFORM).

clc; % Clear command window

% Define the input sequence x (time domain signal)


x = [1, 2, 3, 4, 0, 0, 0, 0]; % Input sequence with 4 non-zero values
followed by 4 zeros
N = 8; % Length of the sequence/number of DFT points

% Compute the discrete Fourier transform (DFT)


% This transforms the time-domain signal into frequency domain
y = fft(x, N);

% Calculate magnitude and phase of the DFT result


mag = abs(y); % Magnitude spectrum (absolute value of complex
numbers)
phase = angle(y); % Phase spectrum (angle of complex numbers
in radians)

% Create the index vector for plotting (0 to N-1)


n = 0:1:N-1;

% Create a new figure window


figure;

% First subplot - Display the input sequence (time domain)


subplot(2, 2, 1);
stem(n, x);
xlabel('n');
ylabel('x(n)');
title('Input Sequence (Time Domain)');
% Second subplot - Display the magnitude spectrum
subplot(2, 2, 2);
stem(n, mag);
xlabel('n');
ylabel('Magnitude');
title('Magnitude Spectrum');

% Third subplot - Display the phase spectrum


subplot(2, 2, 3);
stem(n, phase);
xlabel('n');
ylabel('Phase (radians)');
title('Phase Spectrum');

% Display the DFT result in the command window


disp('DFT Result:');
disp(y);
CODE 4.(INVERSE DISCRETE F.T).
clc; % Clear command window

% Define the input sequence x


x = [4, 3, 2, 1, 0, 0, 0, 0]; % Input sequence with 4 non-zero values
and 4 zeros
N = 8; % Length of the sequence/number of DFT points

% Compute the inverse discrete Fourier transform (IDFT)


% This gives us the time-domain representation of the frequency-
domain signal x
y = ifft(x, N);

% Calculate magnitude and phase of the IDFT result


mag = abs(y); % Magnitude (absolute value) of complex numbers
phase = angle(y); % Phase (angle) of complex numbers in radians

% Create the index vector for plotting (0 to N-1)


k = 0:1:N-1;

% Create a new figure window


figure;

% First subplot - Display the input sequence (frequency domain)


subplot(2, 2, 1);
stem(k, x);
xlabel('k');
ylabel('x(k)');
title('Input Sequence (Frequency Domain)');

% Second subplot - Display the magnitude of IDFT result


subplot(2, 2, 2);
stem(k, mag);
xlabel('k');
ylabel('Magnitude');
title('Magnitude of IDFT Result');

% Third subplot - Display the phase of IDFT result


subplot(2, 2, 3);
stem(k, phase);
xlabel('k');
ylabel('Angle (radians)'); % Corrected from 'angel' to 'Angle'
title('Phase of IDFT Result');

% Display the IDFT result (y) in the command window


disp('IDFT Result:');
disp(y);

You might also like