Experiment No. 1 AIM: MATLAB Code To Perform ASK (Amplitude Shift Keying) Modulation and Demodulation
Experiment No. 1 AIM: MATLAB Code To Perform ASK (Amplitude Shift Keying) Modulation and Demodulation
1
AIM: MATLAB Code to perform ASK (Amplitude Shift Keying) modulation and demodulation
MATLAB CODE:
clc;
x=[ 1 0 0 1 1 0 1]; % Binary Information
bp=.000001; % bit period
disp(' Binary information at Transmitter :');
disp(x);
% Representation of transmitting binary information as digital signal
bit=[];
for n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t1=bp/100:bp/100:100*length(x)*(bp/100);
subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('Transmitting information as digital signal');
%Binary-ASK modulation
A1=10; % Amplitude of carrier signal for information 1
A2=5; % Amplitude of carrier signal for information 0
br=1/bp; % bit rate
f=br*10; % carrier frequency
t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for (i=1:1:length(x))
if (x(i)==1)
y=A1*cos(2*pi*f*t2);
else
y=A2*cos(2*pi*f*t2);
end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)');
title('Waveform for Binary ASK Modulation');
% Binary ASK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y=cos(2*pi*f*t); % carrier siignal
mm=y.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z=trapz(t4,mm); % integration
zz=round((2*z/bp));
if(zz>7.5) % logic level = (A1+A2)/2=7.5
a=1;
else
a=0;
end
mn=[mn a];
end
disp(' Binary information at Receiver :');
disp(mn);
% Representation of binary information as digital signal achieved after ASK
% demodulation
bit=[];
for n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t4=bp/100:bp/100:100*length(mn)*(bp/100);
subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('Received information as digital signal after Binary ASK Demodulation');
OUTPUT
EXPERIMENT NO. 2
AIM: MATLAB Code to perform PSK (Phase Shift Keying) modulation and demodulation
MATLAB CODE:
clc;
x=[ 1 0 0 1 1 0 1]; % Binary Information
bp=.000001; % bit period
disp(' Binary information at Transmitter :');
disp(x);
% Representation of transmitting binary information as digital signal
bit=[];
for n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t1=bp/100:bp/100:100*length(x)*(bp/100);
subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('Transmitting information as digital signal');
% Binary-PSK modulation
A=5; % Amplitude of carrier signal
br=1/bp; % bit rate
f=br*2; % carrier frequency
t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for (i=1:1:length(x))
if (x(i)==1)
y=A*cos(2*pi*f*t2);
else
y=A*cos(2*pi*f*t2+pi); %A*cos(2*pi*f*t+pi) means -A*cos(2*pi*f*t)
end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)');
title('Waveform for Binary PSK Modulation');
% Binary PSK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y=cos(2*pi*f*t); % carrier siignal
mm=y.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z=trapz(t4,mm); % integration
zz=round((2*z/bp));
if(zz>0) %logic level = (A+A)/2=0 because A*cos(2*pi*f*t+pi)=-A*cos(2*pi*f*t)
a=1;
else
a=0;
end
mn=[mn a];
end
disp(' Binary information at Receiver :');
disp(mn);
% Representation of Binary information as digital signal achieved after PSK
% demodulation
bit=[];
for n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t4=bp/100:bp/100:100*length(mn)*(bp/100);
subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('Received information as digital signal after Binary PSK Demodulation');
OUTPUT
EXPERIMENT NO. 3
AIM: MATLAB Code to perform FSK (Frequency Shift Keying) modulation and demodulation
MATLAB CODE:
clc;
x=[ 1 0 0 1 1 0 1]; % Binary Information
bp=.000001; % bit period
disp(' Binary information at Transmitter :');
disp(x);
% Representation of transmitting binary information as digital signal
bit=[];
for n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t1=bp/100:bp/100:100*length(x)*(bp/100);
subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('Transmitting information as digital signal');
% Binary-FSK modulation
A=5; % Amplitude of carrier signal
br=1/bp; % bit rate
f1=br*8; % carrier frequency for information as 1
f2=br*2; % carrier frequency for information as 0
t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for (i=1:1:length(x))
if (x(i)==1)
y=A*cos(2*pi*f1*t2);
else
y=A*cos(2*pi*f2*t2);
end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)');
title('Waveform for Binary FSK Modulation');
% Binary FSK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y1=cos(2*pi*f1*t); % carrier siignal for information 1
y2=cos(2*pi*f2*t); % carrier siignal for information 0
mm=y1.*m((n-(ss-1)):n);
mmm=y2.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z1=trapz(t4,mm); % integration
z2=trapz(t4,mmm); % integration
zz1=round(2*z1/bp);
zz2= round(2*z2/bp);
if(zz1>A/2) % logic level= (0+A)/2 or (A+0)/2 or 2.5
a=1;
else(zz2>A/2)
a=0;
end
mn=[mn a];
end
disp(' Binary information at Receiver :');
disp(mn);
%Representation of binary information as digital signal achieved after
%demodulation
bit=[];
for n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t4=bp/100:bp/100:100*length(mn)*(bp/100);
subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('Received information as digital signal after Binary FSK Demodulation');
EXPERIMENT NO. 4
AIM: MATLAB Code to perform Pulse Code modulation and demodulation (PCM)
MATLAB CODE:
clc;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
% % Signal Generation
% x=0:1/100:4*pi;
% y=8*sin(x); % Amplitude Of signal is 8v
% subplot(2,2,1);
% plot(x,y);grid on;
% Sampling Operation
x=0:2*pi/n1:4*pi; % n1 number of samples have to be selected
s=8*sin(x);
subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude');
xlabel('Time');
subplot(3,1,2);
stem(s);grid on; title('Sampled Signal'); ylabel('Amplitude'); xlabel('Time');
% Quantization Process
vmax=8;
vmin=-vmax;
del=(vmax-vmin)/L;
part=vmin:del:vmax; % level are between vmin and vmax with difference of del
code=vmin-(del/2):del:vmax+(del/2); % Contain Quantized values
[ind,q]=quantiz(s,part,code); % Quantization process
% ind contain index number and q contain quantized values
l1=length(ind);
l2=length(q);
for i=1:l1
if(ind(i)~=0) % To make index as binary decimal so started from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2
if(q(i)==vmin-(del/2)) % To make quantize value in between the levels
q(i)=vmin+(del/2);
end
end
subplot(3,1,3);
stem(q);grid on; % Display the Quantize values
title('Quantized Signal');
ylabel('Amplitude');
xlabel('Time');
% Encoding Process
figure
code=de2bi(ind,'left-msb'); % Convert the decimal to binary
k=1;
for i=1:l1
for j=1:n
coded(k)=code(i,j); % convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
subplot(2,1,1); grid on;
stairs(coded); % Display the encoded signal
axis([0 100 -2 3]); title('Encoded Signal');
ylabel('Amplitude');
xlabel('Time');
% Demodulation Of PCM signal
qunt=reshape(coded,n,length(coded)/n);
index=bi2de(qunt','left-msb'); % getback the index in decimal form
q=del*index+vmin+(del/2); % getback Quantized values
subplot(2,1,2); grid on;
plot(q); % Plot Demodulated signal
title('Demodulated Signal');
ylabel('Amplitude');
xlabel('Time');
EXPERIMENT NO. 5
EXPERIMENT NO.6
AIM: MATLAB Code to perform Number of bit errors and bit error rate (BER)
output
>> BitErrorRateComputationExample
x=
0 0
0 0
0 0
0 0
y=
0 0
0 0
0 0
1 1
numerrs =
numerrs =
1 1
numerrs =
numerrs =
2
EXPERIMENT NO. 7
AIM: MATLAB Code to Configure Eb/No for AWGN Channels with Coding
N = 15; % R-S codeword length in symbols
K = 9; % R-S message length in symbols
M = 16; % Modulation order
rsEncoder = comm.RSEncoder('CodewordLength',N,'MessageLength',K, ...
'BitInput',true);
pskModulator = comm.PSKModulator('ModulationOrder',M,'BitInput',true);
rsDecoder = comm.RSDecoder('CodewordLength',N,'MessageLength',K, ...
'BitInput',true);
pskDemodulator = comm.PSKDemodulator('ModulationOrder',M,'BitOutput',true);
codeRate = K/N;
bitsPerSymbol = log2(M);
UncodedEbNo = 6;
CodedEbNo = UncodedEbNo + 10*log10(codeRate);
channel = comm.AWGNChannel('BitsPerSymbol',bitsPerSymbol);
channel.EbNo = CodedEbNo
output
>> AWGN
channel =
EbNo: 3.7815
BitsPerSymbol: 4
SignalPower: 1
SamplesPerSymbol: 1
EXPERIMENT NO. 8
AIM: MATLAB Code to perform MSK Signal Recovery
%% MSK Signal Recovery
% Model channel impairments such as timing phase offset, carrier frequency
% offset, and carrier phase offset for a minimum shift keying (MSK) signal.
% Use |comm.MSKTimingSynchronizer| and |comm.CarrierSynchronizer| System
% objects to synchronize such signals at the receiver. The MSK timing
% synchronizer recovers the timing offset, while a carrier synchronizer
% recovers the carrier frequency and phase offsets.
%%
% Initialize system variables by running the MATLAB script
% |configureMSKSignalRecoveryEx|. Define the logical control variable
% |recoverTimingPhase| to enable timing phase recovery, and
% |recoverCarrier| to enable carrier frequency and phase recovery.
configureMSKSignalRecoveryEx;
recoverTimingPhase = true;
recoverCarrier = true;
%% Modeling Channel Impairments
% Specify the sample delay, |timingOffset|, that the channel model applies.
% Create a variable fractional delay object to introduce the timing delay
% to the transmitted signal.
%%
timingOffset = 0.2;
varDelay = dsp.VariableFractionalDelay;
%%
% Create a |comm.PhaseFrequencyOffset| System object to introduce carrier
% phase and frequency offsets to a modulated signal. Because the MSK
% modulator upsamples the transmitted symbols, set the |SampleRate|
% property to the ratio of the |samplesPerSymbol| and the sample time,
% |Ts|.
%%
freqOffset = 50;
phaseOffset = 30;
pfo = comm.PhaseFrequencyOffset(...
'FrequencyOffset',freqOffset, ...
'PhaseOffset',phaseOffset, ...
'SampleRate',samplesPerSymbol/Ts);
%%
% Create a |comm.AWGNChannel| System object to add white Gaussian noise to
% the modulated signal. The noise power is determined by the |EbNo|
% property, that is the bit energy to noise power spectral density ratio.
% Because the MSK modulator generates symbols with 1 Watt of power, set the
% signal power property of the AWGN channel System object to 1.
%%
EbNo = 20 + 10*log10(samplesPerSymbol);
chAWGN = comm.AWGNChannel(...
'NoiseMethod','Signal to noise ratio (Eb/No)', ...
'EbNo',EbNo,...
'SignalPower',1, ...
'SamplesPerSymbol',samplesPerSymbol);
%% Timing Phase, Carrier Frequency, and Carrier Phase Synchronization
% Create an MSK timing synchronizer to recover symbol timing phase using a
% fourth-order nonlinearity method.
%%
timeSync = comm.MSKTimingSynchronizer(...
'SamplesPerSymbol',samplesPerSymbol, ...
'ErrorUpdateGain',0.02);
%%
% Create a carrier synchronizer to recover both carrier frequency and
% phase. Because the MSK constellation is QPSK with a 0-degree phase
% offset, set the |comm.CarrierSynchronizer| accordingly.
%%
phaseSync = comm.CarrierSynchronizer(...
'Modulation','QPSK', ...
'ModulationPhaseOffset','Custom', ...
'CustomPhaseOffset',0, ...
'SamplesPerSymbol',1);
%% Stream Processing Loop
% The simulation modulates data using MSK modulation. The modulated symbols
% pass through the channel model, which applies timing delay, carrier
% frequency and phase shift, and additive white Gaussian noise. The
% receiver performs timing phase and carrier frequency and phase recovery.
% Finally, the signal symbols are demodulated and the bit error rate is
% calculated. The |plotResultsMSKSignalRecoveryEx| script generates scatter
% plots in this order to show these effects:
%
% # Channel impairments
% # Timing synchronization
% # Carrier synchronization
%
% At the end of the simulation, the example displays the timing phase,
% frequency, and phase estimates as a function of simulation time.
%%
for p = 1:numFrames
%------------------------------------------------------------------------
% Generate and modulate data
%------------------------------------------------------------------------
txBits = randi([0 1],samplesPerFrame,1);
txSym = modem(txBits);
%------------------------------------------------------------------------
% Transmit through channel
%------------------------------------------------------------------------
%
% Add timing offset
rxSigTimingOff = varDelay(txSym,timingOffset*samplesPerSymbol);
%
% Add carrier frequency and phase offset
rxSigCFO = pfo(rxSigTimingOff);
%
% Pass the signal through an AWGN channel
rxSig = chAWGN(rxSigCFO);
%
% Save the transmitted signal for plotting
plot_rx = rxSig;
%
%------------------------------------------------------------------------
% Timing recovery
%------------------------------------------------------------------------
if recoverTimingPhase
% Recover symbol timing phase using fourth-order nonlinearity
% method
[rxSym,timEst] = timeSync(rxSig);
% Calculate the timing delay estimate for each sample
timEst = timEst(1)/samplesPerSymbol;
else
% Do not apply timing recovery and simply downsample the received
% signal
rxSym = downsample(rxSig,samplesPerSymbol);
timEst = 0;
end
%------------------------------------------------------------------------
% Carrier frequency and phase recovery
%------------------------------------------------------------------------
if recoverCarrier
% The following script applies carrier frequency and phase recovery
% using a second order phase-locked loop (PLL), and removes phase
ambiguity
[rxSym,phEst] = phaseSync(rxSym);
removePhaseAmbiguityMSKSignalRecoveryEx;
freqShiftEst = mean(diff(phEst)/(Ts*2*pi));
phEst = mod(mean(phEst),360); % in degrees
else
freqShiftEst = 0;
phEst = 0;
end
%------------------------------------------------------------------------
% Demodulate the received symbols
%------------------------------------------------------------------------
rxBits = demod(rxSym);
%------------------------------------------------------------------------
% Calculate the bit error rate
%------------------------------------------------------------------------
errorStats = BERCalc(txBits,rxBits);
%------------------------------------------------------------------------
% Plot results
%------------------------------------------------------------------------
plotResultsMSKSignalRecoveryEx;
end
%%
% Display the bit error rate and the total number of symbols processed by
% the error rate calculator.
%%
BitErrorRate = errorStats(1)
TotalNumberOfSymbols = errorStats(3)
%% Conclusion and Further Experimentation
% The recovery algorithms are demonstrated by using constellation plots
% taken after timing, carrier frequency, and carrier phase synchronization.
%
% Open the script to create a writable copy of this example and its
% supporting files. Then, to show the effects of the recovery algorithms,
% you can enable and disable the logical control variables |
recoverTimingPhase| and
% |recoverCarrier| and rerun the simulation.
output
>> MSKSignalRecoveryExample
BitErrorRate =
2.0001e-06
TotalNumberOfSymbols =
499982
EXPERIMENT NO. 9
AIM: MATLAB Code to perform DQPSK Signal in AWGN
dqpskmod = comm.DQPSKModulator('BitInput',true);
dqpskdemod = comm.DQPSKDemodulator('BitOutput',true);
channel = comm.AWGNChannel('EbNo',6,'BitsPerSymbol',2);
errorRate = comm.ErrorRate('ComputationDelay',1);
for counter = 1:100
txData = randi([0 1],100,1);
modSig = dqpskmod(txData);
rxSig = channel(modSig);
rxData = dqpskdemod(rxSig);
errorStats = errorRate(txData,rxData);
end
ber = errorStats(1)
numErrors = errorStats(2)
numBits = errorStats(3)
output
>> DQPSK
ber =
0.0174
numErrors =
174
numBits =
9999
EXPERIMENT NO. 10
AIM: MATLAB Code to perform OQPSK Signal in AWGN
oqpskmod = comm.OQPSKModulator('BitInput',true);
oqpskdemod = comm.OQPSKDemodulator('BitOutput',true);
channel = comm.AWGNChannel('EbNo',4,'BitsPerSymbol',2);
errorRate = comm.ErrorRate('ReceiveDelay',2);
for counter = 1:300
txData = randi([0 1],100,1);
modSig = oqpskmod(txData);
rxSig = channel(modSig);
rxData = oqpskdemod(rxSig);
errorStats = errorRate(txData,rxData);
end
ber = errorStats(1)
numErrors = errorStats(2)
numBits = errorStats(3)
output
>> OQPSK
ber =
numErrors =
numBits =
29998