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

Chap1 2

This document loads and analyzes a speech signal over 1.5 seconds. It quantizes the original signal, calculates the quantization noise power and SNR. It then compresses, quantizes, and expands the signal using mu-law compression. It calculates the quantization noise power and SNR for the compressed signal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chap1 2

This document loads and analyzes a speech signal over 1.5 seconds. It quantizes the original signal, calculates the quantization noise power and SNR. It then compresses, quantizes, and expands the signal using mu-law compression. It calculates the quantization noise power and SNR for the compressed signal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

clear;

% 1. Load speech signal


Fs = 4000;
[mSpeech,Fs] = audioread("MaleSpeech-16-4-mono-20secs.wav");
% sound(mSpeech,Fs)
% Consider the speech signal in 1.5s
t = 0:1/Fs:1.5;
plot(t,mSpeech(1:length(t)),'LineWidth',2);
hold on

% 2. Quantize the sample signal


L = 16; %the number of quantization levels
V_p = 0.5625; %the peak voltage of signal
% Determine the single quantile interval ?-wide
q = V_p / (L - 1); % Exact equation for uniform quantization
s_q_2 = quan_uni(mSpeech(1:length(t)),q); % Uniform quantization
% Plot the sample signal and the quantization signal
plot(t,s_q_2,'ro','MarkerSize',6,'MarkerEdgeColor','r','MarkerFaceColor','r');

% 3. Calculate the average quantization noise power,...


% the average power of the sample signal and SNR
e_uni = mSpeech(1:length(t)) - s_q_2; % error between sample signal and quantized
signal
pow_noise_uni = sum(e_uni.^2) / length(t);
pow_sig = sum(mSpeech(1:length(t)).^2) / length(t);
SNR_a_uni = pow_sig ./ pow_noise_uni;

% Plot 'mSpeech' and 𝑠𝑞2


plot(t, mSpeech(1:length(t)), 'LineWidth', 2);
hold on;
plot(t, s_q_2, 'ro', 'MarkerSize', 6, 'MarkerEdgeColor', 'r', 'MarkerFaceColor',
'r');
legend('mSpeech', '𝑠𝑞2');
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Signal vs. Quantized Signal');

%--------compression-------------
% 5. Compress the sample signal ‘mSpeech’
mu = 255; % for u-law, standard value
y_max = V_p;
x_max = V_p;
s_c_5 = sign(mSpeech(1:length(t))).*(log(1 + mu*abs(mSpeech(1:length(t)))/y_max) /
log(1 + mu)) * x_max; % Compress using mu-law
% Plot the compress signal;
plot(t,s_c_5);
% 6. Quantize the compress signal and plot the quantized signal
s_q_6 = quan_uni(s_c_5,q);
plot(t,s_q_6,'b^','MarkerSize',6,'MarkerEdgeColor','b','MarkerFaceColor','b');

% 7. Expand the quantized signal


s_e_7 = sign(s_q_6).*(y_max/mu).*( (1 + mu).^abs(s_q_6/x_max) - 1); % Expand mu-law
quantized signal
plot(t,s_e_7,'g*','MarkerSize',6,'MarkerEdgeColor','g','MarkerFaceColor','g');
legend('Sample signal','Uniform quantized values','Compress signal','Compress
quantized values','Nouniform quantized values');

% 9. Calculate the average quantization noise power,...


% the average power of the analog signal and SNR
e_com = mSpeech(1:length(t)) - s_e_7;
pow_noise_com = sum(e_com.^2) / length(t);
SNR_a_com = pow_sig / pow_noise_com;

function quan_sig = quan_uni(sig,q)


for i = 1:length(sig)
quan_sig(i) = quant(sig(i),q);
d = sig(i) - quan_sig(i);
if d == 0
quan_sig(i) = quan_sig(i) + q/2;
elseif (d > 0) && (abs(d) < q/2)
quan_sig(i) = quan_sig(i) + q/2;
elseif (d > 0) && (abs(d) >= q/2)
quan_sig(i) = quan_sig(i) - q/2;
elseif (d < 0) && (abs(d) < q/2)
quan_sig(i) = quan_sig(i) - q/2;
elseif (d < 0) && (abs(d) >= q/2)
quan_sig(i) = quan_sig(i) + q/2;
end
end
end

You might also like