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

Code PCM

The document describes the process of pulse code modulation (PCM) for digitizing an analog audio signal. It reads an audio file, samples and quantizes the signal, calculates the quantization error and signal-to-noise ratio, then plots the original, sampled, quantized, and reconstructed signals. It also defines functions for PCM encoding that take an analog signal, sampling frequency, number of bits, and returns the digital signal, bit rate, mean squared error, quantization step size, and noise.

Uploaded by

Mehdi Hssein
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Code PCM

The document describes the process of pulse code modulation (PCM) for digitizing an analog audio signal. It reads an audio file, samples and quantizes the signal, calculates the quantization error and signal-to-noise ratio, then plots the original, sampled, quantized, and reconstructed signals. It also defines functions for PCM encoding that take an analog signal, sampling frequency, number of bits, and returns the digital signal, bit rate, mean squared error, quantization step size, and noise.

Uploaded by

Mehdi Hssein
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

clear all

close all

close all hidden

% Clearing all input and output from the Command Window display giving us a clean screen. clc

% Opening the file 'TEOTH.mp3' in the read access mode. fid = fopen ('TEOTH.mp3','r');

% % % m

Generating the input signal 'm(t)' by reading the binary data in 16 bit integer format from the specified file and writing it into a matrix 'm(t)'. = fread (fid,'int16');

% Defining the count for efficiency. count = 8500;

% Calculating maximum value of the input signal 'm(t)'. Mp = max (m)

% Setting number of bits in a symbol. bits = 8;

% Defining the number of levels of uniform quantization. levels = 2^bits;

% Calculating the step size of the quantization. step_size = (2*Mp)/levels

Fs = 8000; Ts = 1; No_Samples = (2*Fs)+Ts;

time = [1:Fs/64];

% Calculating the bit rate. bit_rate = 8000*bits;

% Quantizing the input signal 'm(t)'. for k = 1:No_Samples, samp_in(k) = m(k*Ts); quant_in(k) = samp_in(k)/step_size;

error(k) = (samp_in(k) - quant_in(k))/No_Samples; end

signS = sign (m); quant_out = quant_in; for i = 1:count, S(i) = abs (quant_in(i)) + 0.5; quant_out(i) = signS(i)*round(S(i))*step_size; end

% Calculating the quantization noise 'Nq'. Nq = ((Mp)^2)/(3*((levels)^2))

% Calculating signal to noise ratio 'SNR'. SNR = 1.5*((levels)^2) Gms = log10(SNR)

subplot(4,1,1); plot(time,m(time)); title('Message Signal'); xlabel('Time'); ylabel('m(t)'); grid on;

subplot(4,1,2); stem(time,quant_in(time),'r'); title('Quantized Speech Signal'); xlabel('Time'); ylabel('Levels'); grid on; subplot(4,1,3); plot(time,quant_out(time)); title('PCM Speech Signal'); xlabel('Time'); ylabel('PC Signal'); grid on;

subplot(4,1,4); plot(time,error(time)); title('Error Signal'); xlabel('Time'); ylabel('Error(t)'); grid on;

% Removing all variables, functions, and MEX-files from memory, leaving the % workspace empty. clear all

% Removing all variables, functions, and MEX-files from memory, leaving the % workspace empty.

clear all

% Deleting all figures whose handles are not hidden. close all

% Deleting all figures including those with hidden handles. close all hidden

% Clearing all input and output from the Command Window display giving us a clean screen. clc % Opening the file ... [m,Fs] = wavread('F:\test.wav'); %counting how many samples are there samples_count = (size(m)); % Calculating maximum value of the input signal 'm(t)'. Mp = max (m); % Setting number of bits in a symbol. bits = 6; % Defining the number of levels of uniform quantization. levels = 2^bits; % Calculating the step size of the quantization. step_size = (2*Mp)/levels; %No_ofSamples -> getting all the samples from the file, but for % 2seconds max if (samples_count(1) > 2*Fs ) No_Samples = (2*Fs); else No_Samples = samples_count(1); end No_Samples = samples_count(1); %Creating the time vector -> this shows the sample index so far time = [1:No_Samples]; % Quantizing the input signal 'm(t)'. for k = 1:No_Samples, samp_in(k) = m(k); quant_in(k) = round(samp_in(k)/step_size)*step_size; error(k) = (samp_in(k) - quant_in(k)); end % Calculating for k = 1:No_Samples, reconstructed(k) = quant_in(k)+step_size/2; end % Plotting the input signal 'm(t)'. %figure; subplot(5,1,1);

plot(time/Fs*1000,m(time)); %note that time depends on the sampling frequency title('Original Signal'); xlabel('Time (ms)'); ylabel('m(t)'); grid on;

% Plotting the quantized signal 'quant_in(t)'. %figure; subplot(5,1,2); stem(time/Fs*1000,quant_in(time),'r'); title('Quantized Speech Signal'); xlabel('Time(ms)'); ylabel('Levels'); grid on; % Plotting the error signal 'error(t)'. subplot(5,1,3); plot(time/Fs*1000,error(time)); title('Quantization Error'); xlabel('Time(ms)'); ylabel('Error(t)'); grid on; % Plotting the error signal 'error(t)'. subplot(5,1,3); plot(time/Fs*1000,error(time)); title('Quantization error signal'); xlabel('Time(ms)'); ylabel('r(t)'); grid on; % Plotting the reconstructed signal 'error(t)'. subplot(5,1,4); plot(time/Fs*1000,reconstructed(time)); title('Reconstructed signal'); xlabel('Time(ms)'); ylabel('r(t)'); grid on;

% Plotting the error signal %figure; subplot(5,1,5); plot(time/Fs*1000,reconstructed(time)-samp_in(time)); title('Error'); xlabel('Time(ms)'); ylabel('PC Signal'); grid on; %calculate the Singla to Noise ration btw the original and the %reconstructed signal %power of original signal Ps = sum(samp_in.*samp_in/2); %power of noise noise = reconstructed - samp_in; Pn = sum(noise.*noise/2); %signal to noise ratio SNR = 10*log(Ps/Pn)

% Removing all variables, functions, and MEX-files from memory, leaving the % workspace empty. clear all

% Deleting all figures whose handles are not hidden. close all

% Deleting all figures including those with hidden handles. close all hidden

% Clearing all input and output from the Command Window display giving us a clean screen. clc

% Opening the file 'TEOTH.mp3' in the read access mode. fid = fopen ('TEOTH.mp3','r');

% % % m

Generating the input signal 'm(t)' by reading the binary data in 16 bit integer format from the specified file and writing it into a matrix 'm(t)'. = fread (fid,'int16');

% Defining the count for efficiency. count = 8500;

% Calculating maximum value of the input signal 'm(t)'. Mp = max (m)

% Setting number of bits in a symbol. bits = 8;

% Defining the number of levels of uniform quantization. levels = 2^bits;

% Calculating the step size of the quantization. step_size = (2*Mp)/levels

% Setting the sampling frequency. % because the audio signal has a maximum frequency of 4K and according to % Nyquist criteria, we get the following sampling frequency. Fs = 8000;

% Setting the sampling instant. Ts = 1;

% Setting the number of samples to be used. No_Samples = (2*Fs)+Ts;

% Define the time vector for the calculations. time = [1:Fs/64];

% Calculating the bit rate. bit_rate = 8000*bits;

% Quantizing the input signal 'm(t)'. for k = 1:No_Samples, samp_in(k) = m(k*Ts); quant_in(k) = samp_in(k)/step_size; error(k) = (samp_in(k) - quant_in(k))/No_Samples; end

% Indicating the sign of the input signal 'm(t)' and calculating the % quantized signal 'quant_out'. signS = sign (m); quant_out = quant_in; for i = 1:count, S(i) = abs (quant_in(i)) + 0.5; quant_out(i) = signS(i)*round(S(i))*step_size; end

% Calculating the quantization noise 'Nq'. Nq = ((Mp)^2)/(3*((levels)^2))

% Calculating signal to noise ratio 'SNR'. SNR = 1.5*((levels)^2) Gms = log10(SNR)

% Plotting the input signal 'm(t)'. %figure; subplot(4,1,1); plot(time,m(time)); title('Message Signal'); xlabel('Time'); ylabel('m(t)'); grid on;

% Plotting the quantized signal 'quant_in(t)'. %figure; subplot(4,1,2); stem(time,quant_in(time),'r'); title('Quantized Speech Signal'); xlabel('Time'); ylabel('Levels'); grid on;

% Plotting the PCM signal 's_out(t)'. %figure; subplot(4,1,3);

plot(time,quant_out(time)); title('PCM Speech Signal'); xlabel('Time'); ylabel('PC Signal'); grid on;

% Plotting the error signal 'error(t)'. subplot(4,1,4); plot(time,error(time)); title('Error Signal'); xlabel('Time'); ylabel('Error(t)'); grid on;

% Removing all variables, functions, and MEX-files from memory, leaving the % workspace empty. clear all

function [y Bitrate MSE Stepsize QNoise]=pcm(A,fm,fs,n) %A=amplitute of cosine signal %fm=frequency of cosine signal %fs=sampling frequency %n= number of bits per sample %MSE=Mean Squar error, QNoise=Quantization Noise %Example [y Bitrate MSE Stepsize QNoise]=pcm(2,3,20,3) %If you have any problem or feedback please contact me @ %%=============================================== % NIKESH BAJAJ % Asst. Prof., Lovely Professional University, India % Almameter: Aligarh Muslim University, India % +919915522564, [email protected] %%=============================================== t=0:1/(100*fm):1; x=A*cos(2*pi*fm*t); %---Sampling----ts=0:1/fs:1; xs=A*cos(2*pi*fm*ts); %xs Sampled signal %--Quantization--x1=xs+A; x1=x1/(2*A); L=(-1+2^n); % Levels x1=L*x1; xq=round(x1); r=xq/L; r=2*A*r; r=r-A; %r quantized signal %----Encoding--y=[]; for i=1:length(xq) d=dec2bin(xq(i),n); y=[y double(d)-48]; end %Calculations MSE=sum((xs-r).^2)/length(x); Bitrate=n*fs; Stepsize=2*A/L; QNoise=((Stepsize)^2)/12; subplot(4,1,1); plot(t,x,'linewidth',2) title('Sampling') ylabel('Amplitute') xlabel('Time t(in sec)') hold on stem(ts,xs,'r','linewidth',2) hold off legend('Original Signal','Sampled Signal'); subplot(4,1,2); stem(ts,x1,'linewidth',2) title('Quantization') ylabel('Levels L')

hold on stem(ts,xq,'r','linewidth',2) plot(ts,xq,'--r') plot(t,(x+A)*L/(2*A),'--b') grid hold off legend('Sampled Signal','Quantized Signal'); subplot(4,1,3); stairs([y y(length(y))],'linewidth',2) title('Encoding') ylabel('Binary Signal') xlabel('bits') axis([0 length(y) -1 2]) grid

You might also like