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

Day_2 Explain

The document outlines a MATLAB script for processing an audio signal, including initialization, loading an input signal, adding noise, and performing amplitude modulation. It explains the steps involved in setting parameters, generating signals, and visualizing both time-domain and frequency-domain representations. The code demonstrates how to simulate real-world noise effects and visualize the modulation process of an audio signal.

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)
9 views

Day_2 Explain

The document outlines a MATLAB script for processing an audio signal, including initialization, loading an input signal, adding noise, and performing amplitude modulation. It explains the steps involved in setting parameters, generating signals, and visualizing both time-domain and frequency-domain representations. The code demonstrates how to simulate real-world noise effects and visualize the modulation process of an audio signal.

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/ 11

1) Sound Signal

1. Initialization:
K.A
clear;
clc;
close all;

Explanation:
• clear : Removes all variables from the workspace, freeing up system memory.
• clc : Clears the command window, giving you a clean slate to see your code's
output.
• close all : Closes all open figure windows, which is useful if you had plots or GUI
windows open.

_____________________________________________________
2. Setting the Sampling Frequency:
fs=100000;
dt=1/fs;

Explanation:
• fs=100000; : This sets the sampling frequency (fs) to 100 kHz. This means that the
signal is sampled 100,000 times per second.
• dt=1/fs; : This calculates the time interval between each sample, which is the
reciprocal of the sampling frequency. dt is useful if you need to plot the signal over
time, though it's not used further in this script.

_____________________________________________________
3. Loading the Input Signal:
load Input_Signal;

Explanation:
• load Input_Signal; : This command loads a variable named Input_Signal from a file
named Input_Signal.mat into the MATLAB workspace. This file is assumed to
contain the audio signal data that you want to process.
4. Playing the Original Signal:
disp('the orignal signal')
sound(Input_Signal,fs,16)

Explanation:
• disp('the original signal'): Displays the text "the original signal" in the command
window, indicating that the original audio signal will be played next.
• sound(Input_Signal,fs,16): This function plays the audio signal stored in
Input_Signal. The parameters are:

- Input_Signal: The audio signal data.


- fs: The sampling frequency, which controls the playback speed.
- 16: The bit depth, which in this case is 16 bits per sample.

_____________________________________________________
Source Code:
clear
clc
close all
fs=100000;
dt=1/fs; % help to draw
load Input_Signal; % load the sound from pc
disp('the orignal signal') % Show for User
sound(Input_Signal,fs,16)
2) Add Noise to Sound Signal
5. Adding Noise and Playing Noisy Signals:
snr=[3 10]; % signal power / noise power
for i=1:length(snr)
noisy_signal=awgn(Input_Signal,snr(i),'measured');
disp('The noisy signal')
sound(noisy_signal,fs,16)
end

Explanation:
• snr=[3 10]; : This defines an array of SNR (Signal-to-Noise Ratio) values, 3 dB and 10
dB. The SNR is a measure of how much noise is present in the signal compared to
the original signal. A higher SNR means less noise.
• for i=1:length(snr): This loop iterates over the SNR values. length(snr) returns the
number of elements in the snr array, which is 2 in this case.
• Inside the loop:
• noisy_signal=awgn(Input_Signal,snr(i),'measured');: This function adds white
Gaussian noise to the Input_Signal based on the SNR value snr(i):
o awgn : function in MATLAB is a tool to simulate real-world noise effects on a
signal, allowing you to see how a signal would perform in the presence of
noise.
o Input_Signal: The original signal.
o snr(i): The current SNR value from the array.
o 'measured': Specifies that the function should measure the power of
Input_Signal to adjust the noise level accordingly.
• disp('The noisy signal'): Displays the text "The noisy signal" to indicate that the
noisy version of the signal is about to be played.
• sound(noisy_signal,fs,16): Plays the noisy signal using the same sampling
frequency and bit depth as before.
• End : keyword signals to MATLAB that the loop is complete. After end, MATLAB
moves on to the next piece of code outside the loop, if there is any.
Source Code:
clear
clc
close all
fs=100000;
dt=1/fs; % help to draw
load Input_Signal; % load the sound from pc
disp('the orignal signal') % Show for User
sound(Input_Signal,fs,16)
snr=[3 10]; % signal power / noise power
for i=1:length(snr)
noisy_signal=awgn(Input_Signal,snr(i),'measured');
disp('The noisy signal')
sound(noisy_signal,fs,16)
end
The Effect of Code:
- The effect of the code will appear when the code file be at the same file of
“Input_Signal” Sound file.
- The Run of the code you will listen “Modern Academy for Engineering and
Technology”.
3) Modulation
1. Initialization:
clear;
clc;
close all;

Explanation:
• clear : Removes all variables from the workspace, freeing up system memory.
• clc : Clears the command window, giving you a clean slate to see your code's
output.
• close all : Closes all open figure windows, which is useful if you had plots or GUI
windows open.

_____________________________________________________
2. Parameters:
Am=2;
Ac=4;
fm=3400;
fc=20000;
fs=5*fc;
Tm=1/fm;
cycle=3;
T=cycle*Tm;
dt=1/fs;
t=0:dt:T-dt;
n=length(t);
df=fs/n;
f=-fs/2:df:(fs/2)-df;
Explanation:
1. Amplitudes:
- Am : Amplitude of the message signal.
- Ac : Amplitude of the carrier signal.

2. Frequencies:
- fm : Frequency of the message signal.
- fc : Frequency of the carrier signal.

3. Sampling:
- fs = 5 * fc : Sampling frequency, which is 5 times the carrier frequency (i.e., `fs = 100000 Hz`).

4. Time Parameters:
- Tm = 1 / fm : Period of the message signal (time for one cycle).
- cycle : Number of cycles of the message signal to simulate.
- T = cycle * Tm: Total simulation time, which covers three cycles of the message signal.

5. Time Vector:
- dt = 1 / fs : Time step between samples.
- t = 0 : dt : T-dt : Time vector, starting from 0 to `T-dt`.

6. FFT and Frequency Domain:


- n = length(t): Number of samples in the time vector.
- df = fs / n : Frequency resolution for the FFT.
- f = -fs/2 : df : (fs/2) – df : Frequency vector for plotting in the frequency domain.
3. Transmitter Components:
load Input_Signal
mt = Input_Signal.';
ct = Ac * cos(2 * pi * fc * t);
xt = (Ac + mt) .* cos(2 * pi * fc * t);

Explanation:
Loading the Input Signal:

• load Input_Signal: Loads the Input_Signal from a .mat file.


• mt = Input_Signal.';: Transposes the signal so that it aligns with the time vector t. This represents
the message signal in the time domain.
• Transpose (.'): Flips the dimensions of the matrix/vector.
Generating the Carrier Signal:

• ct : The carrier signal, a cosine wave with amplitude Ac and frequency fc.
Modulation (Amplitude Modulation):

• The modulation signal xt is created by varying the amplitude of the carrier signal according to the
message signal mt.
• (Ac + mt) : creates a modulated amplitude that varies with the message signal.

_____________________________________________________
4. Frequency Domain Representation (FFT):
mf = abs(fftshift(fft(mt, n))) / n;
cf = abs(fftshift(fft(ct, n))) / n;
xf = abs(fftshift(fft(xt, n))) / n;

Explanation:
• The fft function computes the Fast Fourier Transform (FFT) of the signals.
• fftshift centers the zero frequency component in the middle of the spectrum.
• The result is normalized by dividing by n, the number of samples, to ensure the amplitude is
correct.
• mf : : The Fourier transform of the message signal.
• cf : The Fourier transform of the carrier signal.
• xf : The Fourier transform of the modulated signal.
5. Plotting the Signals:

Time Domain Plots Frequency Domain Plots


subplot(4,2,1) subplot(4,2,2)
plot(t,mt) plot(f, mf)
xlabel('Time'); ylabel('M(t)'); title('The Information xlabel('Frequency'); ylabel('M(f)'); title('The
Signal in TD'); Information Signal in FD');
grid on xlim([-fs fs])
grid on
subplot(4,2,3)
plot(t,ct) subplot(4,2,4)
xlabel('Time'); ylabel('C(t)'); title('The Carrier Signal plot(f, cf)
in TD'); xlabel('Frequency'); ylabel('C(f)'); title('The Carrier
grid on Signal in FD');
xlim([-fs fs])
subplot(4,2,5) grid on
plot(t,xt)
xlabel('Time'); ylabel('X(t)'); title('The Modulation subplot(4,2,6)
Signal in TD'); plot(f, xf)
grid on xlabel('Frequency'); ylabel('X(f)'); title('The
Modulation Signal in FD');
xlim([-fs fs])
grid on

Explanation: Explanation:

The subplot function is used to create a grid of These plots show the frequency domain
plots. Each plot displays a signal in the time representation of the signals:
• mf : The Fourier transform of the message
domain:
signal.
• mt: The message signal. • cf : The Fourier transform of the carrier
signal.
• ct: The carrier signal. • xf : The Fourier transform of the modulated
• xt: The modulated signal. signal.
• xlim([-fs fs]): ensures that the frequency
axis is displayed symmetrically around zero.
Source Code
clc;
clear;
close all;
Am=2; % Amplitude of message
Ac=4; % Amplitude of carrier
fm=3400; % Frequency of message for one cycle
fc=20000; % Frequency of carrier for one cycle
fs=5*fc; %number of samples to draw
Tm=1/fm; %Time for one cycle
cycle=3;
T=cycle*Tm; %total time for all cycle % Total time of simulation
dt=1/fs; % Step in time (time for one sample)
t=0:dt:T-dt; %Sample start from time zero to dt & end atT"total time"-dt"Change in
time"
n=length(t); % Number of frequency points (Number of samples)
df=fs/n; % Frequency resolution
f=-fs/2:df:(fs/2)-df; % Frequency vector
load Input_Signal
mt=Input_Signal.';
ct=Ac*cos(2*pi*fc*t);
xt=(Ac+mt).*cos(2*pi*fc*t);
mf=abs(fftshift(fft(mt,n)))/n;
cf=abs(fftshift(fft(ct,n)))/n;
xf=abs(fftshift(fft(xt,n)))/n;
subplot(4,2,1)
plot(t,mt)
xlabel('Time');ylabel('M(t)');title('The Information Signal in TD');
grid on
subplot(4,2,3)
plot(t,ct)
xlabel('Time');ylabel('C(t)');title('The Carrier Signal in TD');
grid on
subplot(4,2,5)
plot(t,xt)
xlabel('Time');ylabel('X(t)');title('The Modulation Signal in TD');
grid on
subplot(4,2,2)
plot(f,mf)
xlabel('Frequency');ylabel('M(f)');title('The Information Signal in FD');
xlim([-fs fs])
grid on
subplot(4,2,4)
plot(f,cf)
xlabel('Frequency');ylabel('C(f)');title('The Carrier Signal in FD');
xlim([-fs fs])
grid on
subplot(4,2,6)
plot(f,xf)
xlabel('Frequency');ylabel('X(f)');title('The Modulation Signal in FD');
xlim([-fs fs])
grid on
Summary
- The code simulates the process of amplitude modulation by taking an input
message signal and modulating it with a carrier signal. It then visualizes both
the time-domain waveforms and their corresponding frequency-domain
spectra, providing insight into how the modulation affects the signal.

1. Top Row (Left to Right):


• The Information Signal in Time Domain (TD): This shows the original message
signal (M(t)), which is a sine wave.
• The Information Signal in Frequency Domain (FD): This shows the frequency
components of the message signal (M(f)).
2. Middle Row (Left to Right):
• The Carrier Signal in Time Domain (TD): This displays the carrier signal (C(t)),
which is a higher frequency sine wave.
• The Carrier Signal in Frequency Domain (FD): This shows the frequency
components of the carrier signal (C(f)).
3. Bottom Row (Left to Right):
• The Modulation Signal in Time Domain (TD): This is the amplitude-modulated
signal (X(t)), which combines the carrier with the message.
• The Modulation Signal in Frequency Domain (FD): This shows the frequency
components of the modulated signal (X(f)), highlighting the carrier and sidebands
created by modulation.

You might also like