DSP Assignment1111
DSP Assignment1111
INSTITUTE OF TECHNOLOGY
DEPARTEMENT OF COMPUTER ENGINEERING
Group member Id no
1. Natnael Solomon GUR/41137/13
2.Wubshet Ayellew GUR/03113/14
3. Yitbarek Alemu GUR/22870/13
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
% 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
)
(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);
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
function DSPAAS()
% Initialize variables
audioData = [];
fs = 8000; % Fixed sampling rate
quantizedSpeech = [];
bits = 8;
% 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
% Resample to 8000 Hz
if origFs ~= fs
audio = resample(audio, fs, origFs);
end
audioData = audio;
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(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');
function quantizeAndUpdatePlots(bits)
quantizedSpeech = quantize_signal(audioData, bits);
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
--