Audio compression is used to reduce the size of audio files. What is the main goal of this process?
Think about why we want smaller audio files but still want them to sound good.
The main goal of audio compression is to reduce the file size so it takes less space and bandwidth, but still keep the sound quality as close to the original as possible.
Consider this simple Python code that calculates the compression ratio of an audio file:
original_size = 5000 # in KB compressed_size = 1250 # in KB compression_ratio = original_size / compressed_size print(round(compression_ratio, 2))
What will be printed?
original_size = 5000 # in KB compressed_size = 1250 # in KB compression_ratio = original_size / compressed_size print(round(compression_ratio, 2))
Compression ratio is original size divided by compressed size.
The compression ratio is 5000 divided by 1250, which equals 4.0.
Given a numpy array representing raw audio data with shape (44100, 2) for 1 second stereo audio sampled at 44.1kHz, after compression the data is downsampled by a factor of 4 and converted to mono by averaging channels.
What is the shape of the compressed audio data array?
import numpy as np raw_audio = np.random.rand(44100, 2) compressed_audio = raw_audio[::4].mean(axis=1) print(compressed_audio.shape)
Downsampling by 4 reduces samples by 4 times. Averaging channels reduces 2 channels to 1.
Downsampling reduces 44100 samples to 11025 (44100/4). Averaging the 2 channels results in a single channel, so shape is (11025,).
You have two frequency spectrum plots of the same audio: one original and one after lossy compression. Which plot likely shows the compressed audio?
Lossy compression often removes high-frequency details to save space.
Lossy compression reduces or removes some high-frequency sounds, so the compressed audio spectrum looks smoother with fewer high-frequency spikes.
Consider this Python code snippet that attempts to compress audio data by quantization:
import numpy as np audio = np.array([0.1, 0.5, 0.9]) quantized = audio // 0.5 print(quantized)
What error will this code produce when run?
import numpy as np audio = np.array([0.1, 0.5, 0.9]) quantized = audio // 0.5 print(quantized)
Check if floor division (//) works with float operands in numpy arrays.
NumPy supports the floor division operator (//) for float NumPy arrays and float scalars. It computes the floor of the division element-wise: 0.1//0.5 = floor(0.2) = 0.0, 0.5//0.5 = floor(1.0) = 1.0, 0.9//0.5 = floor(1.8) = 1.0. Thus, no error, output: [0. 1. 1.].
