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

Lab 2 Audio Sampling and Quantization Levels

Uploaded by

farhan020813
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)
13 views

Lab 2 Audio Sampling and Quantization Levels

Uploaded by

farhan020813
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/ 5

CSC567

Lab 2

Audio Sampling and Quantization Levels with Sine Waves

Objective:

To investigate how different sampling rates and quantization levels affect the quality and
representation of audio signals.

Note:

Sampling rate:
• Refers to the number of samples taken per second when converting a continuous signal
into a discrete signal.
• It is usually expressed in hertz (Hz). For example, a sampling rate of 44.1 kHz means
44,100 samples are taken each second.

Quantization level:
• The process of mapping a continuous range of values to a finite set of discrete levels.
• For example, an analog audio signal may have an infinite range of amplitudes, but
amplitudes use a limited number of discrete values in quantization.

Materials Needed:
• MATLAB

Procedure:

1. Generate Sine Waves

• Create a sine wave at a specific frequency (e.g., 440 Hz, which is the musical note A4)

Fs_original = 44100; % Original sampling rate (in Hz)


t = 0:1/Fs_original:1; % Time vector for 1 second
f = 440; % Frequency of the sine wave (in Hz)
y = sin(2 * pi * f * t); % Generate the sine wave
figure;
plot(t, y);
title(['Waveform at ', num2str(Fs_original), ' Hz']);
xlabel('Time (s)');
ylabel('Amplitude');
2. Play the Original Sound:

sound(y, Fs_original); % Play the original sound

3. Experiment with Different Sampling Rates:


• Choose several sampling rates (e.g., 8000 Hz, 16000 Hz, and 22050 Hz).
• For each sampling rate, downsample the original sine wave and plot the waveform.

sampling_rates = [44100, 22050, 11025, 8000]; % Example sampling rates


for rate = sampling_rates
% Downsample the sine wave
y_downsampled = downsample(y, round(Fs_original/rate));

% Time vector for downsampled sound


t_downsampled = (0:length(y_downsampled)-1)/rate;

% Plot the waveform


figure;
plot(t_downsampled, y_downsampled);
title(['Waveform at ', num2str(rate), ' Hz']);
xlabel('Time (s)');
ylabel('Amplitude');

% Play the downsampled sound


sound(y_downsampled, rate);
pause(length(y_downsampled)/rate + 1);% Pause for sound to finish
end
4. Experiment with Different Quantization Levels:

• Quantize the sine wave to various bit depths (e.g., 8-bit, 16-bit, and 24-bit).
• For each quantization level, quantize the sine wave and plot the waveform

Fs_original = 44100; % Original sampling rate (in Hz)


t = 0:1/Fs_original:1; % Time vector for 1 second
f = 440; % Frequency of the sine wave (in Hz)
y = sin(2 * pi * f * t); % Generate the sine wave
figure;
plot(t, y);
title(['Waveform at ', num2str(Fs_original), ' Hz']);
xlabel('Time (s)');
ylabel('Amplitude');

quant_levels = [8, 16, 24]; % Example quantization levels


for bits = quant_levels
% Quantization
y_quantized = round(y * (2^(bits-1) - 1)); % Scale to
quantization levels
y_quantized = y_quantized / (2^(bits-1) - 1); % Normalize

% Play the quantized sound


sound(y_quantized, Fs_original);
pause(length(y)/Fs_original + 1); % Pause for sound to finish

% Plot the waveform


figure;
plot(t, y_quantized);
title(['Waveform at ', num2str(bits), ' bits']);
xlabel('Time (s)');
ylabel('Amplitude');
end
5. Calculate the Signal to Noise Ratio (SNR) to measure the signal quality of different
quantization levels used. The SNR is calculated in decibels (dB) using the formula:

𝑆𝑖𝑔𝑛𝑎𝑙 𝑝𝑜𝑤𝑒𝑟
SNR(dB) = 10 log10 ( )
𝑁𝑜𝑖𝑠𝑒 𝑝𝑜𝑤𝑒𝑟
where

Signal Power: Calculated as the mean of the squared quantized signal.


Noise Power: Calculated as the mean of the squared difference between the original and
quantized signals.

Fs = 44100; % Sampling rate (Hz)


duration = 1; % Duration of the audio signal (seconds)
f = 440; % Frequency of the sine wave (Hz)
quant_levels = [8, 12, 16, 24]; % Quantization levels (bits)

% Generate the original sine wave


t = 0:1/Fs:duration; % Time vector
y_original = sin(2 * pi * f * t); % Sine wave

% Initialize SNR results


snr_results = zeros(length(quant_levels), 1);

for j = 1:length(quant_levels)
bits = quant_levels(j); % Current quantization level

% Quantization
y_quantized = round(y_original * (2^(bits-1) - 1)) / (2^(bits-1)
- 1); % Normalize

% Calculate SNR
signal_power = mean(y_quantized.^2); % Signal power
noise_power = mean((y_original - y_quantized).^2); % Noise power
snr = 10 * log10(signal_power / noise_power); % SNR in dB

% Store the result


snr_results(j) = snr;
end

% Display the results

disp('SNR Results (dB) for Different Quantization Levels:');

disp(array2table(snr_results, 'VariableNames', {'SNR_dB'}, ...

'RowNames', arrayfun(@(x) sprintf('%d-bit', x), quant_levels,


'UniformOutput', false)));
Lab Assignments

Answer the following questions: -

1. What is the purpose of audio sampling in digital signal processing?

2. How does the Nyquist theorem relate to sampling rates, and what is its significance in
preventing aliasing?

3. What are the differences between low, medium, and high sampling rates in terms of
sound quality and fidelity?

4. How does quantization affect the dynamic range of an audio signal?

5. What are the consequences of using a low bit depth for quantization when converting
an analog signal to digital?

6. How does oversampling help improve the quality of digitally recorded audio?

7. In what ways can quantization errors manifest in digital audio, and how can they impact
sound quality?

8. Why is 44.1 kHz commonly used as a standard sampling rate for audio CDs?

9. What is the relationship between sampling rates and quantization levels in the context
of audio signal representation?

10. How can the choice of sampling rate and quantization level influence the file size of a
digital audio recording?

You might also like