Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
WAV Audio File Handling with SciPy
📖 Scenario: You have recorded a short sound clip and saved it as a WAV audio file. You want to analyze this audio file by loading it into your program, checking its properties, and then extracting some useful information.
🎯 Goal: Learn how to load a WAV audio file using SciPy, check its sample rate and data shape, and extract the first 100 audio samples.
📋 What You'll Learn
Use SciPy's wavfile module to read WAV files
Access the sample rate and audio data from the file
Extract a slice of the audio data
Print the sample rate and the extracted audio samples
💡 Why This Matters
🌍 Real World
Audio processing is common in music apps, voice assistants, and sound analysis tools. Loading and inspecting WAV files is the first step in these tasks.
💼 Career
Understanding how to handle audio files is useful for data scientists working in audio analytics, speech recognition, and multimedia applications.
Progress0 / 4 steps
1
Load the WAV audio file
Write code to import wavfile from scipy.io and read the WAV file named example.wav using wavfile.read. Store the sample rate in a variable called sample_rate and the audio data in a variable called audio_data.
SciPy
Hint
Use from scipy.io import wavfile to import the module. Then use wavfile.read('example.wav') to read the file.
2
Check the sample rate and audio data shape
Write code to create two variables: rate that stores the value of sample_rate, and shape that stores the shape of audio_data using its shape attribute.
SciPy
Hint
Assign rate = sample_rate and shape = audio_data.shape.
3
Extract the first 100 audio samples
Write code to create a variable called first_100_samples that contains the first 100 elements of audio_data using slicing.
SciPy
Hint
Use slicing like audio_data[:100] to get the first 100 samples.
4
Print the sample rate and first 100 samples
Write code to print the sample rate stored in rate and the first 100 audio samples stored in first_100_samples. Use two separate print statements.
SciPy
Hint
Use print(rate) and print(first_100_samples) to display the values.
Practice
(1/5)
1. What does the function scipy.io.wavfile.read return when you load a WAV audio file?
easy
A. The file size and duration
B. Only the audio data as a list
C. The sample rate and the audio data as a NumPy array
D. The audio format and bit depth
Solution
Step 1: Understand the function purpose
scipy.io.wavfile.read is designed to load WAV files and extract audio information.
Step 2: Identify the returned values
It returns two things: the sample rate (how many samples per second) and the audio data as a NumPy array.
Final Answer:
The sample rate and the audio data as a NumPy array -> Option C
Quick Check:
read() returns (rate, data) [OK]
Hint: Remember read() gives rate and data array [OK]
Common Mistakes:
Thinking it returns only audio data
Confusing sample rate with file size
Expecting metadata like format or bit depth
2. Which of the following is the correct way to import the WAV file reading function from scipy?
easy
A. from scipy.io import wavfile
B. import scipy.wavfile.read
C. from scipy import wavfile.read
D. import wavfile from scipy.io
Solution
Step 1: Recall correct import syntax
Python imports use 'from module import function_or_submodule' format.
Step 2: Match with scipy structure
The correct way is to import the wavfile submodule from scipy.io as from scipy.io import wavfile.
Final Answer:
from scipy.io import wavfile -> Option A
Quick Check:
Correct import syntax = from scipy.io import wavfile [OK]
Hint: Use 'from scipy.io import wavfile' to access read/write [OK]
Common Mistakes:
Trying to import read directly
Using dot notation incorrectly in import
Swapping import order
3. What will be the output shape of the data array when you read a stereo WAV file with 44100 samples per channel using scipy.io.wavfile.read?
medium
A. (88200,)
B. (44100, 2)
C. (44100,)
D. (2, 44100)
Solution
Step 1: Understand stereo audio data shape
Stereo audio has two channels, so data shape is (samples, channels).
Step 2: Calculate shape for 44100 samples
With 44100 samples per channel and 2 channels, shape is (44100, 2).
Final Answer:
(44100, 2) -> Option B
Quick Check:
Stereo shape = (samples, 2) [OK]
Hint: Stereo data shape is (samples, 2) always [OK]
Common Mistakes:
Confusing channels and samples order
Assuming shape is (2, samples)
Thinking stereo data is flattened
4. You try to save a NumPy array with float values using scipy.io.wavfile.write but get an error. What is the likely cause?
medium
A. The file path is incorrect
B. The sample rate is missing
C. The array shape is (samples, 2) instead of (2, samples)
D. The array must be integer type, not float
Solution
Step 1: Check data type requirements for write()
scipy.io.wavfile.write expects integer arrays (e.g., int16) for audio data.
Step 2: Identify error cause
Using float arrays causes errors because WAV format stores integers, so conversion is needed.
Final Answer:
The array must be integer type, not float -> Option D
Quick Check:
write() needs int arrays [OK]
Hint: Convert floats to int before writing WAV [OK]
Common Mistakes:
Ignoring data type and writing floats directly
Forgetting sample rate argument
Misunderstanding array shape requirements
5. You want to double the speed of a WAV audio file using scipy.io.wavfile. Which approach correctly achieves this?
hard
A. Double the sample rate value and write the original data unchanged
B. Read the file, then write only every second sample to a new file
C. Halve the sample rate value and write the original data unchanged
D. Reverse the audio data array and write it with the original sample rate