0% found this document useful (0 votes)
7 views15 pages

DSP Assignment1111

The document describes a MATLAB program developed by a group from the University of Gondar for analyzing speech signal quantization and its effect on Signal-to-Noise Ratio (SNR). It features a graphical user interface (GUI) that allows users to load audio files, adjust bit depth from 3 to 15 bits, and visualize the original and quantized signals while calculating SNR. The analysis highlights the trade-off between bit depth and signal quality, demonstrating that higher bit depths improve audio clarity but increase storage and processing costs.

Uploaded by

wubshet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

DSP Assignment1111

The document describes a MATLAB program developed by a group from the University of Gondar for analyzing speech signal quantization and its effect on Signal-to-Noise Ratio (SNR). It features a graphical user interface (GUI) that allows users to load audio files, adjust bit depth from 3 to 15 bits, and visualize the original and quantized signals while calculating SNR. The analysis highlights the trade-off between bit depth and signal quality, demonstrating that higher bit depths improve audio clarity but increase storage and processing costs.

Uploaded by

wubshet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIVERSITY OF GONDAR

INSTITUTE OF TECHNOLOGY
DEPARTEMENT OF COMPUTER ENGINEERING

DIGITAL SIGNAL PROCESSING GROUP ASSIGNMENT.

Group member Id no
1. Natnael Solomon GUR/41137/13
2.Wubshet Ayellew GUR/03113/14
3. Yitbarek Alemu GUR/22870/13

Submitted to:- Mr.Belaineh.E


Submission date:- Feb
20, 2017
Speech Quantization and SNR Analysis

Overview
This MATLAB program analyzes the effect of quantization on speech
signals by varying the bit depth from 3 to 15 bits. The program
provides a graphical user interface (GUI) to load an audio file,
quantize the speech signal, and visualize the original and quantized
signals. Additionally, it calculates and plots the Signal-to-Noise Ratio
(SNR) for different bit depths to demonstrate the impact of
quantization on signal quality.
Key Features
GUI-Based Application:
The program features a user-friendly GUI with buttons, a slider, and
plots for easy interaction.
Users can load an audio file, adjust the bit depth, and play the
original or quantized audio.
Quantization
The speech signal is quantized using a specified bit depth (ranging
from 3 to 15 bits).
The quantization process normalizes the signal, rounds it to the
nearest quantization level, and scales it back to its original range.
Signal Visualization
The original and quantized speech signals are plotted for visual
comparison.The SNR vs. bit depth graph is dynamically updated as
the bit depth changes.
SNR Calculation
The Signal-to-Noise Ratio (SNR) is calculated for each bit depth to
quantify the quality of the quantized signal.
SNR is plotted against the number of bits to show the relationship
between bit depth and signal quality.
GUI Components
Control Panel
Contains buttons for loading audio, playing the original and
quantized signals, and a slider to adjust the bit depth.
Plots
Original Signal Plot: Displays the waveform of the loaded audio
signal.
Quantized Signal Plot: Displays the waveform of the quantized
signal for the selected bit depth.
SNR Plot: Shows the SNR (in dB) as a function of the number of bits
used for quantization.
Functions
DSPAAS()
The main function that initializes the GUI and sets up the control
panel, plots, and callbacks.
loadAudioCallback()
Loads an audio file (.wav or .mp3), normalizes it, and updates the
original signal plot.
bitDepthSliderCallback()
Updates the bit depth based on the slider value and re-quantizes the
signal.
playQuantizedCallback(): Plays the quantized audio signal.
playOriginalCallback(): Plays the original audio signal.
quantizeAndUpdatePlots(): Quantizes the signal for the given bit
depth, updates the quantized signal plot, and recalculates the SNR.
quantize_signal(): Performs the quantization process by normalizing
the signal, rounding it to the nearest quantization level, and scaling it
back to the original range.
Processing Algorithms
Loads an audio file via the "Load Audio" button. The signal is then
normalized and shown in the "Original Signal Plot."
The program reads the selected audio file (audioread()).
• If the sampling rate differs from 8 kHz, it is resampled to 8000 Hz
using resample().
• The audio is normalized to a range of [-1, 1] by dividing by the
maximum absolute
amplitude.
Resampling
When loading an audio file, it may have a different sampling rate. To
maintain consistency, the program resamples the audio to 8 kHz (a
standard for speech processing). Resampling helps ensure that
processing is applied uniformly across different audio files.
Matlab code for resampling
function loadAudioCallback(~, ~)
[filename, pathname] = uigetfile({'*.mp3;*.wav', 'Audio Files (*.mp3,
*.wav)'}, 'Select an audio file');
if isequal(filename, 0)
disp('User selected Cancel');
return;
end

filepath = fullfile(pathname, filename);


try
[audio, origFs] = audioread(filepath);
audio = audio / max(abs(audio)); % Normalize

% Resample to 8000 Hz
if origFs ~= fs
audio = resample(audio, fs, origFs);
end

Quantization Logic
Quantization is the process of mapping a continuous signal to a
finite set of discrete levels. The number of levels depends on the bit
depth.
Calulating the number of quantization levels: L =
Determine the quantization step: Q = L 2 1

x
Quantize the signal by rounding to the nearest level: XQ  Round ( )  Q
Q
Where “x” is the original audio signal and “xq” is the quantized
signal.
• The quantized waveform is displayed, and the audio is played back.
Matlab code for quantization
function quantized_signal = quantize_signal(signal, bits)
signal_normalized = (signal + 1) / 2;
levels = 2^bits;
quantized_signal = round(signal_normalized * (levels - 1)) / (levels -
1);
quantized_signal = 2 * quantized_signal - 1;
end

Signal-to-Noise Ratio (SNR):


SNR measures the ratio of the power of the original signal to the
power of the quantization noise. Higher SNR indicates better signal
quality.
To analyze the distortion introduced by quantization, the signal-to-
noise ratio (SNR) is
SNR (dB) =
Where:
Signal Power: The power of the original signal.
Noise Power: The power of the noise introduced by
quantization.
SNR  log10 (
x 2

)
 (x  x ) q
2

Where:
• x is the original signal
• xq is the quantized signal
• The denominator represents the power of the quantization noise.
• The SNR plot shows how increasing bit depth improves audio
quality by reducing quantization noise.
Matlab code for SNR plot
function quantizeAndUpdatePlots(bits)
quantizedSpeech = quantize_signal(audioData, bits);

% Update quantized signal plot


plot(quantized3Axes, quantizedSpeech);
title(quantized3Axes, ['Quantized Speech Signal with ', num2str(bits), '
Bits']);

% Update SNR plot


updateSNRPlot();
end

function updateSNRPlot()
% Calculate SNR for 3, 8, and 15 bits
snrValues = zeros(1, 3);
bitDepths = [3, 8, 15];

for i = 1:length(bitDepths)
quantized = quantize_signal(audioData, bitDepths(i));
noise = audioData - quantized;
signalPower = sum(audioData.^2) / length(audioData);
noisePower = sum(noise.^2) / length(noise);
snrValues(i) = 10 * log10(signalPower / noisePower);
end

% Plot SNR values


plot(snrAxes, bitDepths, snrValues, '-o');
xlabel(snrAxes, 'Number of Bits');
ylabel(snrAxes, 'SNR (dB)');
title(snrAxes, 'SNR vs. Number of Bits');
grid(snrAxes, 'on');
end
end
Comparison of 3-bit, 8-bit, and 15-bit Speech

In this section, we analyze the impact of varying bit depths (from


3 to 15 bits) on the quantization of speech signals and the resulting
Signal-to-Noise Ratio (SNR).
Bit Depth and Quantization Levels
 Bit Depth refers to the number of bits used to represent each
sample in the digital signal. The number of quantization levels is
determined by the bit depth, following the formula:
 As the bit depth increases, the number of quantization
levels increases exponentially. This means that the signal
can be represented more accurately, reducing the
quantization error but increased storage and processing
cost.
 Lower bit depth More quantization noise, but reduced data
size
Impact of Bit Depth on Signal Quality

Low Bit Depth (3 bits):


 As shown in Figure , the 3-bit quantized signal exhibits
significant distortion due to the limited number of
quantization levels. At low bit depths, the number of
quantization levels is very limited. This results in a coarse
representation of the signal, leading to significant
quantization noise.
 The quantized signal will have noticeable distortion, and
the original signal's details will be lost.
 The SNR is relatively low, indicating poor signal quality.
 The waveform is step-like, indicating that the signal has
been discretized into a limited number of levels.
 The signal quality is reduced due to the coarse quantization
(only 8 levels).
Medium Bit Depth (8 bits)
 As show in the figure the signal quality is still relatively
high because 8 bits provide a sufficient number of levels to
approximate the original signal closely.
 The quantized signal appears as a step-like waveform,
where the signal is approximated to the nearest
quantization level.
 Moderate distortion, acceptable clarity
 The quantized signal loses some of the fine details of the original
signal, introducing quantization noise.
 Medium SNR, fair quality

High Bit Depth (15 bits):


 At higher bit depths, the number of quantization levels becomes
very large, providing a near-continuous representation of the
signal.
 The quantization noise is minimal, and the quantized signal
closely resembles the original signal.
 The SNR is high, indicating excellent signal quality.
SNR vs. Bit Depth Analysis
At low bit depths (e.g., 3-bit, 8-bit), the SNR values are relatively low
(-4.40 dB and 28.06 dB,
respectively), meaning the signal suffers from noticeable quantization
noise, leading to a
distorted or robotic sound. As the bit depth increases (8-bit, 11-bit,
12-bit), the SNR improves and , making the audio clearer and more
natural.
At higher bit depths (above 12-bit), the SNR continues to rise, but
the improvements become less significant. This follows a logarithmic
pattern, where each additional bit contributes less to quality
enhancement. Beyond 15-bit quantization, the perceptual difference
becomes minimal, as human hearing has limitations in detecting
small noise variations.This figure highlights the fundamental trade-
off in digital audio processing:
• Higher bit depth: Better quality, higher SNR, but increased storage
and processing
cost.
• Lower bit depth: More quantization noise, but reduced data size.
Thus, selecting an appropriate bit depth depends on the required
balance between quality and computational efficiency in a given
application.
Key Observations:
• Lower bit depths introduce significant quantization noise, causing
distortion and reducing
speech clarity.
• As bit depth increases, the quantized signal more closely resembles
the original,
improving perceptual quality.
• Beyond 15-bit quantization, further improvement is minimal, as
human auditory
perception has limitations in distinguishing very high SNR
differences.
• 3-bit: Heavy quantization noise, noticeable distortion, robotic
sound.
• 8-bit: Acceptable quality, some noise, commonly used in telephony.
• 15-bit: Near lossless quality, minimal distortion, similar to the
original signal
Conclusion
This MATLAB program provides a practical tool for understanding
the effects of quantization on speech signals. By varying the bit depth
and observing the resulting SNR, users can gain insights into the
trade-off between bit depth and signal quality. The GUI makes it
easy to interact with the program and visualize the results.

% DSP Group Assignment


% GROUP MEMBERS
% 1. NATNAEL SOLOMON GUR //13
% 2. WUBSHET AYELEW GUR/03113/14
% 3. YITBAREK ALEMU GUR/22870/13

function DSPAAS()
% Initialize variables
audioData = [];
fs = 8000; % Fixed sampling rate
quantizedSpeech = [];
bits = 8;

% Create the main figure window


fig = figure('Position', [100, 85, 1000, 800], 'Name', 'Speech Quantizer',
'NumberTitle', 'off');

% Create a vertical control panel on the left with minimum width


controlPanel = uipanel('Title', 'Controls', 'Position', [0.02, 0.02, 0.10,
0.96]); % Reduced width to 10%

% Load Audio Button


loadButton = uicontrol('Style', 'pushbutton', 'String', 'Load Audio', ...
'Position', [10, 500, 80, 30], 'Parent', controlPanel, ...
'Callback', @loadAudioCallback, ...
'Tooltip', 'Load an audio file (WAV or MP3)');
% Play Original Button
playOriginalButton = uicontrol('Style', 'pushbutton', 'String', 'Play
Original', ...
'Position', [10, 450, 80, 30], 'Parent', controlPanel, ...
'Callback', @playOriginalCallback, ...
'Tooltip', 'Play the original audio');

% Play Quantized Button


playQuantizedButton = uicontrol('Style', 'pushbutton', 'String', 'Play
Quantized', ...
'Position', [10, 400, 80, 30], 'Parent', controlPanel, ...
'Callback', @playQuantizedCallback, ...
'Tooltip', 'Play the quantized audio');

% Compare Quantization Button


compareButton = uicontrol('Style', 'pushbutton', 'String', 'Compare', ...
'Position', [10, 350, 80, 30], 'Parent', controlPanel, ...
'Callback', @compareQuantizationCallback, ...
'Tooltip', 'Compare quantization at 3, 8, and 15 bits');

% Stop Audio Button


stopButton = uicontrol('Style', 'pushbutton', 'String', 'Stop Audio', ...
'Position', [10, 300, 80, 30], 'Parent', controlPanel, ...
'Callback', @stopAudioCallback, ...
'Tooltip', 'Stop audio playback');

% Bit Depth Slider


bitDepthSlider = uicontrol('Style', 'slider', 'Min', 3, 'Max', 15, ...
'Value', 8, 'SliderStep', [1/(15-3), 1/(15-3)], ...
'Position', [10, 250, 80, 20], 'Parent', controlPanel, ...
'Callback', @bitDepthSliderCallback, ...
'Tooltip', 'Adjust the bit depth for quantization');

% Bit Depth Text


bitDepthText = uicontrol('Style', 'text', 'String', 'Bit Depth: 8', ...
'Position', [10, 220, 80, 20], 'Parent', controlPanel);

% Axes for plotting signals


signalAxes = subplot(3, 2, [1, 2]); % Original signal
title('Original Audio');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;

quantized3Axes = subplot(3, 2, 3); % 3-bit quantized signal


title('3-bit Quantized Audio');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;

quantized8Axes = subplot(3, 2, 4); % 8-bit quantized signal


title('8-bit Quantized Audio');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;

quantized15Axes = subplot(3, 2, 5); % 15-bit quantized signal


title('15-bit Quantized Audio');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;

snrAxes = subplot(3, 2, 6); % SNR plot


xlabel('Number of Bits');
ylabel('SNR (dB)');
title('SNR vs. Number of Bits');
grid on;

% Callback functions
function loadAudioCallback(~, ~)
[filename, pathname] = uigetfile({'*.mp3;*.wav', 'Audio Files (*.mp3,
*.wav)'}, 'Select an audio file');
if isequal(filename, 0)
disp('User selected Cancel');
return;
end

filepath = fullfile(pathname, filename);


try
[audio, origFs] = audioread(filepath);
audio = audio / max(abs(audio)); % Normalize

% Resample to 8000 Hz
if origFs ~= fs
audio = resample(audio, fs, origFs);
end

audioData = audio;

% Update signal plot


plot(signalAxes, audioData);
title(signalAxes, 'Original Audio');

% Quantize and plot initial audio


quantizeAndUpdatePlots(bits);
catch ME
errordlg(['Error loading audio file: ' ME.message], 'Error');
return;
end
end

function bitDepthSliderCallback(~, ~)
bits = round(get(bitDepthSlider, 'Value'));
set(bitDepthText, 'String', ['Bit Depth: ', num2str(bits)]);
if ~isempty(audioData)
quantizeAndUpdatePlots(bits);
end
end

function playQuantizedCallback(~, ~)
if ~isempty(quantizedSpeech)
sound(quantizedSpeech, fs);
else
warndlg('Load audio and adjust bit depth first.', 'Warning');
end
end

function playOriginalCallback(~, ~)
if ~isempty(audioData)
sound(audioData, fs);
else
warndlg('Load audio first.', 'Warning');
end
end

function stopAudioCallback(~, ~)
clear sound; % Stop any audio playback
end

function compareQuantizationCallback(~, ~)
if ~isempty(audioData)
% Quantize the audio at 3, 8, and 15 bits
quantized3 = quantize_signal(audioData, 3);
quantized8 = quantize_signal(audioData, 8);
quantized15 = quantize_signal(audioData, 15);

% Plot the original and quantized signals


plot(signalAxes, audioData);
title(signalAxes, 'Original Audio');

plot(quantized3Axes, quantized3);
title(quantized3Axes, '3-bit Quantized Audio');

plot(quantized8Axes, quantized8);
title(quantized8Axes, '8-bit Quantized Audio');

plot(quantized15Axes, quantized15);
title(quantized15Axes, '15-bit Quantized Audio');

% Calculate and update SNR plot


updateSNRPlot();
else
warndlg('Load audio first.', 'Warning');
end
end

function quantizeAndUpdatePlots(bits)
quantizedSpeech = quantize_signal(audioData, bits);

% Update quantized signal plot


plot(quantized3Axes, quantizedSpeech);
title(quantized3Axes, ['Quantized Speech Signal with ', num2str(bits), '
Bits']);

% Update SNR plot


updateSNRPlot();
end

function updateSNRPlot()
% Calculate SNR for 3, 8, and 15 bits
snrValues = zeros(1, 3);
bitDepths = [3, 8, 15];

for i = 1:length(bitDepths)
quantized = quantize_signal(audioData, bitDepths(i));
noise = audioData - quantized;
signalPower = sum(audioData.^2) / length(audioData);
noisePower = sum(noise.^2) / length(noise);
snrValues(i) = 10 * log10(signalPower / noisePower);
end

% Plot SNR values


plot(snrAxes, bitDepths, snrValues, '-o');
xlabel(snrAxes, 'Number of Bits');
ylabel(snrAxes, 'SNR (dB)');
title(snrAxes, 'SNR vs. Number of Bits');
grid(snrAxes, 'on');
end
end

function quantized_signal = quantize_signal(signal, bits)


signal_normalized = (signal + 1) / 2;
levels = 2^bits;
quantized_signal = round(signal_normalized * (levels - 1)) / (levels - 1);
quantized_signal = 2 * quantized_signal - 1;
end

--

You might also like