% DCS Assignmnent 2
% Sagar Suman, 2019197
% Ans 1 -
% Part a) -
% Taking audio signal as input -
[m, fs] = audioread("audio_2019197.mp3");
% above audio is 2 sec in duration.
% Playing the audio -
% sound(y,fs);
m = m(:,1);
% Plotting -
N = length(m);
k = 0:N-1;
t = k/fs; % 1/fs = ts
figure;
plot(t, m,"b");
xlabel('Time (seconds) [t]');
ylabel('Amplitude of audio signal m(t)');
title("Audio signal");
% Part b) -
% Firstly, finding out frequency spectrum of audio signal
% So, that we can know what frequency components are there
% Compute the DFT of the audio signal
M = fft(m);
% Compute the magnitude of the DFT coefficients
M_mag = abs(M);
% Compute the frequency axis for the DFT
freq = k*fs/N;
% Plot the frequency spectrum of the signal
figure;
plot(freq, M_mag,"blue");
xlabel('Frequency (in Hz) [f]');
ylabel('Amplitude |M(f)|');
title("Magnitude spectrum of m(t)")
% Plotting Zoomed in version of M(f)-
figure;
plot(freq,M_mag,"blue");
xlabel(sprintf("Frequency (in Hz) [f]\n " + ...
"\\bfFigure 1. shows our audio signal is bandlimited by\n" + ...
"\\bf fm = 3000 Hz. After which, frequency components\n" + ...
"\\bf are very small\n"));
1
ylabel('Amplitude |M(f)|');
title("Zoomed in Magnitude spectrum of m(t)")
% Setting Xlim
xlim([0,5000]);
% Now from the plot, we can see that M(f) is bandlimited to
% fm = 3000 Hz. After which, frequenct components are very small.
% Hence we have fm = 3000 Hz.
fm = 3000;
% Now choosing three sampling frequency to ensure cases for
% undersampling (fs<2fm),
f_undersampling = 3000;
% and nyquist sampling (fs = 2fm)
f_nyquist = 6000;
% Oversampling (fs >2fm)
f_oversampling = 9000;
% Now performing sampling for each of the cases -
% Using resample function - sample fs/f times in resample(x,fs,f)
%undersampling
m_undersampling = resample(m, f_undersampling, fs);
% nyquist sampling -
m_nyquist = resample(m, f_nyquist, fs);
% Oversampling -
m_oversampling = resample(m, f_oversampling, fs);
%plotting these -
figure;
% Undersampling plot -
subplot(3,1,1);
plot((0:length(m_undersampling)-1)/f_undersampling, m_undersampling,"b");
xlabel('Time (seconds) [t]');
ylabel('Amplitude');
title("Audio signal undersampled at fs = 3000 Hz");
% nyquist sampling plot
subplot(3,1,2);
plot((0:length(m_nyquist)-1)/f_nyquist, m_nyquist,"b");
xlabel('Time (seconds) [t]');
ylabel('Amplitude');
title("Audio signal nyquist at fs = 6000 Hz");
% Oversampling plot -
subplot(3,1,3);
plot((0:length(m_oversampling)-1)/f_oversampling, m_oversampling,"b");
xlabel('Time (seconds) [t]');
ylabel('Amplitude');
title("Audio signal oversampled at fs = 9000 Hz");
% Quantization -
% Using 8 bit quantization -
bits = 8;
% levels
2
levels = 2^bits;
% defining delta ( as range of mp3 files in matlab is -1 to 1)
delta = 2/levels;
% For undersampling -
m_q_undersampling = round((m_undersampling+1)/delta);
% For nyquist sampling-
m_q_nyquist = round((m_nyquist+1)/delta);
% For oversamplingsampling -
m_q_oversampling = round((m_oversampling+1)/delta);
% Generating databitstream -
% For undersampling -
m_b_undersampling = de2bi(m_q_undersampling, "left-msb");
m_b_undersampling = reshape(m_b_undersampling,[], 1);
% For nyquist sampling -
m_b_nyquist = de2bi(m_q_nyquist, "left-msb");
m_b_nyquist = reshape(m_b_nyquist,[], 1);
% For oversampling-
m_b_oversampling = de2bi(m_q_oversampling, "left-msb");
m_b_oversampling = reshape(m_b_oversampling,[], 1);
% PART C - MODULATION
% For 8 QAM -
l = length(m_b_nyquist);
% Ensuring 3 bits
m_8_qam= qammod(m_b_nyquist(1:floor(l/3)*3), 8, "InputType","bit");
% For 16 QAM -
m_16_qam= qammod(m_b_nyquist(1:floor(l/4)*4), 16, "InputType","bit");
% For 32 QAM -
m_32_qam= qammod(m_b_nyquist(1:floor(l/5)*5), 32, "InputType","bit");
% For 64 QAM -
m_64_qam= qammod(m_b_nyquist(1:floor(l/6)*6), 64, "InputType","bit");
% For 128 QAM -
m_128_qam= qammod(m_b_nyquist(1:floor(l/7)*7), 128, "InputType","bit");
% For 256 QAM -
m_256_qam= qammod(m_b_nyquist(1:floor(l/8)*8), 256, "InputType","bit");
% Considering white gaussian noice in system -
% SET SNR HERE
snr = 5;
% AWG in 8 Qam -
m_8_qam_awgn = awgn(m_8_qam, snr);
% AWG in 16 Qam -
m_16_qam_awgn = awgn(m_16_qam, snr);
% AWG in 32 Qam -
m_32_qam_awgn = awgn(m_32_qam, snr);
% AWG in 64 Qam -
m_64_qam_awgn = awgn(m_64_qam, snr);
% AWG in 128 Qam -
m_128_qam_awgn = awgn(m_128_qam, snr);
% AWG in 256 Qam -
m_256_qam_awgn = awgn(m_256_qam, snr);
3
% PART D - Constelation diagram
%plotting subplot for each qam
% 8 qam
scatterplot(m_8_qam);
title("FOR 8 QAM");
% 16 qam
scatterplot(m_16_qam);
title("For 16 qam");
% 32 qam
scatterplot(m_32_qam);
title('for 32 qam');
% 64 qam
scatterplot(m_64_qam);
title('for 64 qam');
% 128 qam
scatterplot(m_128_qam);
title('for 128 qam');
% 256 qam
scatterplot(m_256_qam);
title('for 256 qam');
% NOW plotting for modulate signals with awgn
%plotting subplot for each qam
% 8 qam
scatterplot(m_8_qam_awgn);
title("FOR 8 QAM with AWGN");
% 16 qam
scatterplot(m_16_qam_awgn);
title("For 16 qam with AWGN");
% 32 qam
scatterplot(m_32_qam_awgn);
title('for 32 qam with AWGN');
% 64 qam
scatterplot(m_64_qam_awgn);
title('for 64 qam with AWGN');
% 128 qam
scatterplot(m_128_qam_awgn);
title('for 128 qam with AWGN');
% 256 qam
scatterplot(m_256_qam_awgn);
title('for 256 qam with AWGN');
% PART E - Demodulation and BER -
% Demodulating the recieved signal-
% for 8 qam recieved signal-
r_8_qam_demod = qamdemod(m_8_qam_awgn,8, "OutputType","bit");
% for 16 qam recieved signal-
r_16_qam_demod = qamdemod(m_16_qam_awgn, 16, "OutputType","bit");
% for 32 qam recieved signal-
r_32_qam_demod = qamdemod(m_32_qam_awgn, 32, "OutputType","bit");
% for 64 qam recieved signal-
4
r_64_qam_demod = qamdemod(m_64_qam_awgn, 64, "OutputType","bit");
% for 128 qam recieved signal-
r_128_qam_demod = qamdemod(m_128_qam_awgn, 128, "OutputType","bit");
% for 256 qam recieved signal-
r_256_qam_demod = qamdemod(m_256_qam_awgn, 256, "OutputType","bit");
% Computing bit error rate-
% For 8 qam -
errors = sum(xor(m_b_nyquist(1:floor(l/3)*3) ,r_8_qam_demod));
ber = errors/length(r_8_qam_demod);
fprintf('Bit error rate for 8 QAM : %.10f\n', ber);
% For 16 qam -
errors = sum(xor(m_b_nyquist(1:floor(l/4)*4) ,r_16_qam_demod));
ber = errors/length(r_16_qam_demod);
fprintf('Bit error rate for 16 QAM : %.10f\n', ber);
% For 32 qam -
errors = sum(xor(m_b_nyquist(1:floor(l/5)*5) ,r_32_qam_demod));
ber = errors/length(r_32_qam_demod);
fprintf('Bit error rate for 32 QAM : %.10f\n', ber);
% For 64 qam -
errors = sum(xor(m_b_nyquist(1:floor(l/6)*6),r_64_qam_demod));
ber = errors/length(r_64_qam_demod);
fprintf('Bit error rate for 64 QAM : %.10f\n', ber);
% For 128 qam -
errors = sum(xor(m_b_nyquist(1:floor(l/7)*7) ,r_128_qam_demod));
ber = errors/length(r_128_qam_demod);
fprintf('Bit error rate for 128 QAM :%.10f\n', ber);
% For 256 qam -
errors = sum(xor(m_b_nyquist(1:floor(l/8)*8),r_256_qam_demod));
ber = errors/length(r_256_qam_demod);
fprintf('Bit error rate for 256 QAM : %.10f\n', ber);
% Conclusion -
fprintf(['Through this question, we can see\n' ...
'1. On increasing M for M-QAM-> Bit error rate decreases\n' ...
'2. But we can also observe the fact that, if we increase SNR\n' ...
' at receiver, we are able to reduce the Bit error rate']);
Bit error rate for 8 QAM : 0.0045627530
Bit error rate for 16 QAM : 0.0044819511
Bit error rate for 32 QAM : 0.0040177670
Bit error rate for 64 QAM : 0.0029174238
Bit error rate for 128 QAM :0.0031393846
Bit error rate for 256 QAM : 0.0027356053
Through this question, we can see
1. On increasing M for M-QAM-> Bit error rate decreases
2. But we can also observe the fact that, if we increase SNR
at receiver, we are able to reduce the Bit error rate
5
6
7
8
9
10
11
12
13
Published with MATLAB® R2022b
14