I need someone to detail the following simulation and what would be the results? PS: I am a novice in using MATLAB and would like to understand.

2 views (last 30 days)
bit_rate = 5;
f = 5;
fs = 4000;
x = [0 0 0 1 1 0 1 1 1 0 0 0 1];
t = 0:1/fs:1/bit_rate-1/fs;
time = 0:1/fs:(1/bit_rate*length(x))-1/fs;
% Modulation
signal=[];
forn=1:length(x);
if x(n)==1;
y=1.*cos(2*pi*f*t);
else
y=1.*cos(2*pi*f*t + pi);
end
signal=[signal y];
end
% Demodulation
result = []
j = 0;
fori=1:length(x);
a = trapz(signal(j+1:j+length(t)) .* cos(2*pi*f*t));
b = trapz(signal(j+1:j+length(t)) .* cos(2*pi*f*t + pi));
if a > b;
result = [result 1];
else
result = [result 0];
end
j = j+length(t);
end
display(result);

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 18 Mar 2020
Hi Jeffy,
Here is the annotated code with explanation. This code basically does phase modulation and demodulation.
bit_rate = 5; % how frequent the bits are transmitted, to calculate the bit duration
f = 5; % Frequency of the signal or the carrier
fs = 4000; % Sample rate
x = [0 0 0 1 1 0 1 1 1 0 0 0 1]; % Input bits
t = 0:1/fs:1/bit_rate-1/fs; % Sampling each bit (it turns out to be time duration of one symbol after sampling)
time = 0:1/fs:(1/bit_rate*length(x))-1/fs; % total duration of the signal
% Modulation
signal=[]; % Initialize the variable to store the output signal
for n=1:length(x) % Loop over each bit to perform the phase modulation
if x(n)==1 % If the value of bit is 1, transmit the carrier signal (i.e. cos(*pi*f*t)) without any phase deviation
y=1.*cos(2*pi*f*t);
else % If the value of bit is 0, transmit the carrier signal with a value shifted by pi
y=1.*cos(2*pi*f*t + pi);
end
signal=[signal y]; % Append the signal for all the bits
end
% Demodulation
result = []; % Initialize a variable to store the result
j = 0; % Loop variable to select the duration of each bit
for i=1:length(x) % Perform demodulation for each bits
a = trapz(signal(j+1:j+length(t)) .* cos(2*pi*f*t)); % This integratres the signal multiplied with cos(2*pi*f*t)
b = trapz(signal(j+1:j+length(t)) .* cos(2*pi*f*t + pi)); % This integratres the signal multiplied with cos(2*pi*f*t+pi)
if a > b % Decision threshold
% If the correlation obtained by integration of the signal multiplied
% with cos(2*pi*f*t) is greater than the correlation obtained by
% integration multiplied with cos(2*pi*f*t+pi), Then the decision is
% the transmitted signal is 1 and append this it to the result
result = [result 1];
else
% If the correlation obtained by integration of the signal multiplied
% with cos(2*pi*f*t) is less than or equal the correlation obtained by
% integration multiplied with cos(2*pi*f*t+pi), Then the decision is
% the transmitted signal is 0 and append this it to the result
result = [result 0];
end
j = j+length(t); % loop over all the bits
end
display(result); % Display the demodulated bits
Hope this helps.
Regards,
Sriram

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!