You are on page 1of 18

Lab 3

Amplitude modulation/demodulation in MATLAB

Submitted By: Muhammad Maab Nawaz


FA21-EEE-021

Submitted To: Dr. Ghufran Shafiq

Submission Date: 0 5 /10/2023

In Lab (4) Post Lab (6) Total (10)


Data Presentation Data Analysis Writing Style
TASK#1
Code
close all;
clear all;
clc;
fs=1000;
fm=10;
fc=250;

to = 0.15;
t=0:0.001:5;
m= 1*(0<=t & t<to/3) -2*((to/3)<=t & t< 2*(to/3));%cos(2*pi*fm*t);*
figure();
subplot(2,1,1)
plot(t,m,'black',linewidth=3)
xlim([0 0.2])
title('original signal in time domain')

D= fftshift(fft(m)/length(m));
freq1=linspace(-fs/2,fs/2,length(m));
subplot(2,1,2)
plot(freq1,abs(D),'black',linewidth=2);
title('original signal in freq domain')
xlabel('Freq')

// In this task I made a tone and analyzed its freq spectrum which was even around
w=0.//
TASK#2
Code
close all;
clear all;
clc;
fs=1000;
fm=10;
fc=250;

to = 0.15;
t=0:0.001:5;
m= 1*(0<=t & t<to/3) -2*((to/3)<=t & t< 2*(to/3));%cos(2*pi*fm*t);*
figure();
plot(t,m,'black',linewidth=3)
xlim([0 0.2])
title('original signal in time domain')
figure();
n=cos(2*pi*fc*t);
s=m.*n;
subplot(2,1,1)
plot(t,s,t,m,t,-m,linewidth=3)
xlim([0 0.2])
ylim([-2.2 2.2])
title('modulated signal in time domain')

L= fftshift(fft(s)/length(s));
freq2=linspace(-fs/2,fs/2,length(s));
subplot(2,1,2)
plot(freq2,abs(L),'black',linewidth=3);
ylim([0 0.02])
title('modulated original signal in freq domain')
// In this task I made a modulated the tone by multiplying the convoluted signal with a
high F cos wave and analyzed its freq spectrum which.From the F spectrum it is clear that
the original tone spectrum in replicated and shifed at wc(the carrier F) after being
multipied with high F cos.//

TASK#3
Code
close all;
clear all;
clc;
fs=1000;
fm=10;
fc=250;

to = 0.15;
t=0:0.001:5;
m= 1*(0<=t & t<to/3) -2*((to/3)<=t & t< 2*(to/3));
n=cos(2*pi*fc*t);
s=m.*n;
demodulation=m.*s;
n = 8;
flow =230;
[b,a] = butter(n,flow/(fs/2));
final=filter(b,a,demodulation);
figure();
subplot(3,1,1)
plot(t,final,'black',linewidth=2)
title('demodulated signal in time domain after filter')
xlim([0 0.2])

subplot(3,1,2)
plot(t,demodulation,'black',linewidth=3);
title('demodulated original signal in time domain')
xlim([0 0.12])

X= fftshift(fft(final)/length(final));
freq=linspace(-fs/2,fs/2,length(final));
subplot(3,1,3)
plot(freq,abs(X),'black',linewidth=2);
title('demodulated signal in freq domain after filter')
// In this task I made a modulated the tone by multiplying the convoluted signal with a
high F cos wave and analyzed its freq spectrum which.After filtering out the high F
component ,I got the orignal tone.The choice of the filter was butterworth and I needed
to make lowpass which could filter out F less than Fc(carrier F).so I just multiplied it
with a factor less than 1 to get our low cutoff F.

TASK#4
Normal code
Code
fs=10000;
fm=10;
fc=250;
A=4;

to = 0.15;
t=0:0.0001:10;
m= 1*(0<=t & t<to/3) -2*((to/3)<=t & t< 2*(to/3));
figure();
plot(t,m,'black',linewidth=3)
xlim([0 0.2])
title('original signal in time domain')
n=cos(2*pi*fc*t);
s=m.*n +4*n;
figure();
subplot(2,1,1)
plot(t,s,t,m+A,t,-m-A,'black',linewidth=3)
title('modulated signal(with carrier in time domain)')
xlim([0.0 0.2])
H= fftshift(fft(s)/length(s));
freq2=linspace(-fs/2,fs/2,length(s));
subplot(2,1,2)
plot(freq2,abs(H),'black',linewidth=3);
title('modulated signal(with carrier) in freq domain')
ylim([0 2.2])

demodulation=n.*s;
n = 8;
flow =250;
[b,a] = butter(n,flow/(fs/2));
final=filter(b,a,demodulation)-2;
figure();
subplot(3,1,1)
plot(t,final,'black',linewidth=3)
title('demodulated signal in time domain after filtering')
xlim([0 0.2])

subplot(3,1,2)
plot(t,demodulation,'black',linewidth=3);
title('demodulated signal in time domain before filter')
xlim([0 0.01])
X= fftshift(fft(final)/length(final));
freq=linspace(-fs/2,fs/2,length(final));

X2= fftshift(fft(demodulation)/length(demodulation));
freq3=linspace(-fs/2,fs/2,length(demodulation));
subplot(4,1,4)
plot(freq3,abs(X2),'black',linewidth=3);
ylim([0 4.2])
title('demodulated signal in freq domain before filter')
Function code
function u =system(m,mp,u,fc)

fs=10000;
A=mp./u;
t=0:0.0001:10;
figure();
plot(t,m,'black',linewidth=3)
xlim([0 0.2])
title('original signal in time domain')
n=cos(2*pi*fc*t);
s=m.*n +4*n;
figure();
subplot(2,1,1)
plot(t,s,t,m+A,t,-m-A,'black',linewidth=3)
title('modulated signal(with carrier in time domain)')
xlim([0.0 0.2])

H= fftshift(fft(s)/length(s));
freq2=linspace(-fs/2,fs/2,length(s));
subplot(2,1,2)
plot(freq2,abs(H),'black',linewidth=3);
title('modulated signal(with carrier) in freq domain')
ylim([0 2.2])

demodulation=n.*s;
n = 8;
flow =250;
[b,a] = butter(n,flow/(fs/2));
final=filter(b,a,demodulation)-(A/2);
figure();
subplot(3,1,1)
plot(t,final,'black',linewidth=3)

title('demodulated signal in time domain after filtering')


xlim([0 0.2])

subplot(3,1,2)
plot(t,demodulation,'black',linewidth=3);
title('demodulated signal in time domain before filter')
xlim([0 0.01])

X2= fftshift(fft(demodulation)/length(demodulation));
freq3=linspace(-fs/2,fs/2,length(demodulation));
subplot(4,1,4)
plot(freq3,abs(X2),'black',linewidth=3);
ylim([0 4.2])
title('demodulated signal in freq domain before filter')

Function Call :
t=0:0.0001:10;
u = 0.5;
mp=2;
u = 0.5;
mp=2;
m= cos(2*pi*10*t);
n =system(m,mp,u,250)
//In this task I also sent a transmit carrier. Now a suppressed carrier can be sent by
multiplying it with same high F cos wave and add it to the original signal, but its
amplitude should be correct such that the envelope isn’t in negative and this will cause
𝒎
distortion.( 𝒖 = 𝑨𝒑 )The mp here was 2 and the 𝒖 was 0.5 and after rearranging we get
A=4.We also have to subtract this A=4 from demodulated signal, in order to get the
original signal. In the second task I analyzed the parameters of the code and made it a
function and then in function call, modulated and demodulated a cos wave. //

Post Lab
Code
function a = moddemod(k)
[y, fs] = audioread(k);
FS=500000;
fc=15000;
% t=0:0.0001:10;

t = linspace(0, length(y)/fs, length(y));


figure();
plot(y,'black',linewidth=3)
title('original signal in time domain')
Xx= fftshift(fft(y)/length(y));
freq3=linspace(-FS/2,FS/2,length(y));
figure();
plot(freq3,abs(Xx));
title('audio in freq domain')

n=cos(2*pi*fc*t);
s=ammod(y, 15000, 50000);
audiowrite(beforeTransmissionFile1,y,FS);
figure();
plot(t,s,'black',linewidth=3)
title('modulated signal in time domain)');
xlim([0 0.001])
% H= fftshift(fft(s)/length(s));
% freq2=linspace(-FS/2,FS/2,length(s));
% subplot(2,1,2)
% plot(freq2,abs(H),'black',linewidth=3);
% title('modulated signal(with carrier) in freq domain')
% ylim([0 140000])
demodulation=amdemod(s, 15000, 50000);
[b,a] = butter(10,fs/(FS/2));
final=filter(b,a,demodulation);
figure();
plot(t,final,'black',linewidth=3)
title('demodulated signal in time domain after filtering')
xlim([0 7])
% X12= fftshift(fft(final)/length(final));
% freq12=linspace(-FS/2,FS/2,length(final));
% subplot(2,1,2)
% plot(freq12,abs(X12),'black',linewidth=3);
% ylim([0 6])
% title('demodulated signal in freq domain after filtering')
audiowrite(receviedAmSoundFile1,final,FS);
figure();
plot(t,demodulation,'black',linewidth=3);
title('demodulated signal in time domain before filter')
xlim([0 0.001])
sound(final, fs)
// In this code I didn’t used the conventional method of modulation and
demodulation.Instead I explored the MATLAB built in functions ammod and amdemod
having syntax y = ammod/ amdemod (x, Carrier F, Sampling F); I used a .wav file and I
had to change the time axis.We used t = linspace(0, length(y)/fs, length(y));
Instead of a t to ensure that the sampling rate of the output signal is the same as
the sampling rate of the input signal so that the frequency content of the signal is
preserved. I also faced a length error in modulation and to overcome that I
multiplied the carrier signal with the length of .wav signal because in workspace I
observed that .wav signal was 2 dimensional.

Additional Features:
% function a = moddemod(k)
% [y, fs] = audioread(k);
FS=50000;
fc=15000;
fs=9000;
% t=0:0.0001:10;
bitResolution = 8;
recordTime = 5;
channel = 2;

beforeTransmissionFile1 = strcat(pwd, '\recordedSound1.wav');


receviedAmSoundFile1 = strcat(pwd, '\receivedAMVoice1.wav');

selectInputDeviceID = inputDeviceInformation();
disp('Start recording')
y = audioRecorder(FS,bitResolution,channel,selectInputDeviceID,recordTime);
disp('Stop')

audiowrite(beforeTransmissionFile1,y,FS);
t = linspace(0, length(y)/fs, length(y));
figure();
plot(y,'black',linewidth=3)
title('original signal in time domain')

n=cos(2*pi*fc*t);
s=ammod(y, 15000, 50000);
audiowrite(beforeTransmissionFile1,y,FS);
figure();

plot(t,s,'black',linewidth=3)
title('modulated signal');
demodulation=amdemod(s, 15000, 50000);
[b,a] = butter(10,fs/(FS/2));
final=filter(b,a,demodulation);
figure();
plot(t,final,'black',linewidth=3)
title('demodulated signal in time domain after filtering')
audiowrite(receviedAmSoundFile1,final,FS);
figure();
plot(t,demodulation,'black',linewidth=3);
title('demodulated signal in time domain before filter')
sound(final, FS)
//In this task I explored additional modules and features that could be done with an audio
file modulation and demodulation and I discovered that MATLAB can read your laptops
drivers. So I used that to record voice Realtime, modulate and demodulate it and also save
the recordings in a file. Here is the list of steps I did in order to achieve this task:
1: I made a function inputDeviceInformation to get input device information:

function [inputDeviceNumber] = inputDeviceInformation()


deviceInfo = audiodevinfo;
inputDeviceInfo = struct2table(deviceInfo.input);
disp(inputDeviceInfo);
inputDeviceNumber = input("Select the audio device for recording from list = ");
end
The function inputDeviceInformation() returns the input device number selected by the user
from the list of available input devices. The function first calls the audiodevinfo
function to get information about all the audio devices connected to the system. It then
uses the struct2table() function to convert the input field of the deviceInfo structure to
a table. The table is then displayed to the user. The user is then prompted to select the
audio device for recording from the list of available input devices. The selected device
number is then returned by the function .

2: I made a function audioRecorder to record audio

function [recordedVoice,audioVoice] =
audioRecorder(samplingFrequency,bitResolution,channel,deviceNumber,recordTime)
audioVoice = audiorecorder(samplingFrequency,bitResolution,channel, deviceNumber);
recordblocking(audioVoice,recordTime);
recordedVoice = getaudiodata(audioVoice);

end

The audioRecorder() function takes five arguments:


SamplingFrequency: The sampling frequency of the audio signal to record. ( Sampling
frequency is the number of samples taken per second of an audio signal.)
BitResolution: The bit resolution of the audio signal to record.( Bit resolution is the
number of bits used to represent each sample of an audio signal.)
Channel: The number of channels in the audio signal to record.
DeviceNumber: The device number of the audio input device to record from. (Device number
is the device number of the audio input device to record from. This can be found using the
audiodevinfo() function.)
RecordTime: The amount of time to record the audio signal in seconds.

The function first creates an audiorecorder object using the specified sampling frequency,
bit resolution, channel, and device number. It then calls the recordblocking() function to
record the audio signal for the specified amount of time. Finally, it calls the
getaudiodata() function to get the recorded audio signal as a vector.

3: I then called the required functions in main and made the modulation and demodulation
function.
In main code,first I called the input inputDeviceInformation() to select the desired way
of recording my audio.Then I feeded that as in input to my second function audiorecorder
and and only used one output which was desirable recordedVoice which was a vector
containing the recorded audio signal.The rest was same for modulation and demodulation.
For saving it in a file ,first I used audio audiowrite command to write it on a file and
for saving I used the following syntax. beforeTransmissionFile1=strcat(pwd,
'\recordedSound1.wav'); It creates a string variable called beforeTransmissionFile1 that
contains the full path to the file recordedSound1.wav in the current working directory.
The pwd function returns the current working directory as a string. The strcat() function
concatenates the two strings.

Critical analysis:

In this lab I learnt about amplitude modulation, suppressed, and transmit carrier. I
transmitted a simple tone, cos wave and demodulated the signals. I observed that even
after demodulation and filter, the waveform had a little bit of ripples which was due to
the filter itself. Then I loaded a .wav file and modulated and demodulated it.I also
recorded real time audio signal and performed the same process. The post lab task was
interesting as I got to know of a lot of modules and features of MATLAB after
researching.

You might also like