Bird
Raised Fist0
Signal Processingdata~20 mins

Reading and writing audio files in Signal Processing - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Audio File Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code reading a WAV file?
Given the code below that reads an audio file and prints the sample rate and shape of the data array, what will be printed?
Signal Processing
import numpy as np
from scipy.io import wavfile

sample_rate, data = wavfile.read('test_audio.wav')
print(sample_rate, data.shape)
A44100 (100000, 2)
B44100 (100000,)
C22050 (100000, 2)
DError: File not found
Attempts:
2 left
💡 Hint
Check the sample rate and shape returned by wavfile.read for a stereo file sampled at 44100 Hz.
❓ data_output
intermediate
1:30remaining
What is the shape of the audio data array after reading a mono WAV file?
If you read a mono WAV file using scipy.io.wavfile.read, what will be the shape of the returned data array?
Signal Processing
from scipy.io import wavfile
sample_rate, data = wavfile.read('mono_audio.wav')
print(data.shape)
A(1, 44100)
B(44100,)
C(44100, 1)
DError: data has no shape
Attempts:
2 left
💡 Hint
Mono audio data is a single channel, so the array is one-dimensional.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error when writing audio?
This code tries to write a numpy array to a WAV file but raises a ValueError. What is the cause?
Signal Processing
from scipy.io import wavfile
import numpy as np

sample_rate = 44100
data = np.array([0.1, 0.2, 0.3, 0.4])
wavfile.write('output.wav', sample_rate, data)
AFile path is invalid
BSample rate must be a float, not int
CData array must be integer type, not float
DData array shape is incorrect
Attempts:
2 left
💡 Hint
Check the data type required by wavfile.write for audio data.
🚀 Application
advanced
2:30remaining
How to normalize audio data before writing to WAV?
You have a numpy array of audio samples with float values between -2 and 2. Which option correctly normalizes it to int16 range before saving?
Signal Processing
import numpy as np
from scipy.io import wavfile

float_data = np.array([-2.0, 0.0, 2.0, 1.0])
Aint_data = (float_data / np.max(np.abs(float_data)) * 32767).astype(np.int16)
Bint_data = (float_data / 2 * 32767).astype(np.int16)
Cint_data = (float_data * 32767).astype(np.int16)
Dint_data = float_data.astype(np.int16)
Attempts:
2 left
💡 Hint
Normalization scales the max absolute value to 1 before converting.
🧠 Conceptual
expert
3:00remaining
What happens if you write stereo data as mono in WAV file?
If you have a stereo numpy array with shape (samples, 2) but write only one channel to a WAV file, what is the effect?
AThe WAV file will contain mixed audio from both channels
BThe WAV file will be stereo but second channel will be silent
CThe WAV file will cause a runtime error during writing
DThe WAV file will be mono with only the first channel's audio
Attempts:
2 left
💡 Hint
Writing only one channel means only that channel's data is saved.

Practice

(1/5)
1. What does reading an audio file using a library like soundfile typically provide you?
easy
A. The audio data array and the sample rate
B. Only the file name
C. The audio file size in bytes
D. The duration of the audio in minutes

Solution

  1. Step 1: Understand the purpose of reading audio files

    Reading audio files extracts the sound data and the sample rate for processing.
  2. Step 2: Identify what the library returns

    Libraries like soundfile return the audio data as an array and the sample rate as an integer.
  3. Final Answer:

    The audio data array and the sample rate -> Option A
  4. Quick Check:

    Reading audio files = data + sample rate [OK]
Hint: Remember: reading audio gives data and sample rate [OK]
Common Mistakes:
  • Thinking reading returns only file metadata
  • Confusing sample rate with file size
  • Assuming reading returns audio duration only
2. Which of the following is the correct way to read an audio file using the soundfile library in Python?
easy
A. data, samplerate = soundfile.read('audio.wav')
B. data = soundfile.load('audio.wav')
C. samplerate, data = soundfile.read('audio.wav')
D. soundfile.open('audio.wav')

Solution

  1. Step 1: Recall the soundfile.read function signature

    The function returns two values: data array and sample rate, in that order.
  2. Step 2: Match the correct syntax

    data, samplerate = soundfile.read('audio.wav') correctly assigns data and sample rate from soundfile.read.
  3. Final Answer:

    data, samplerate = soundfile.read('audio.wav') -> Option A
  4. Quick Check:

    Correct syntax = data, samplerate = read(file) [OK]
Hint: Remember order: data first, then sample rate [OK]
Common Mistakes:
  • Swapping data and sample rate order
  • Using non-existent load method
  • Not unpacking returned tuple
3. What will be the output type of the variable data after running this code?
import soundfile as sf
data, samplerate = sf.read('example.wav')
medium
A. An integer representing duration
B. A list of integers
C. A string with audio info
D. A NumPy array of floats

Solution

  1. Step 1: Understand sf.read output

    The soundfile.read function returns audio data as a NumPy array, usually with float values normalized between -1 and 1.
  2. Step 2: Identify the data type

    The variable data will be a NumPy array of floats representing audio samples.
  3. Final Answer:

    A NumPy array of floats -> Option D
  4. Quick Check:

    Audio data = NumPy float array [OK]
Hint: Audio data is a NumPy float array, not list or string [OK]
Common Mistakes:
  • Assuming data is a list instead of array
  • Thinking data is a string or integer
  • Confusing sample rate with data type
4. Identify the error in this code snippet that tries to write audio data to a file:
import soundfile as sf
sf.write('output.wav', samplerate, data)
medium
A. Missing import statement for NumPy
B. Arguments order is incorrect; data should come before samplerate
C. File extension must be .mp3, not .wav
D. No error; code is correct

Solution

  1. Step 1: Check sf.write function signature

    The correct order is sf.write(file, data, samplerate), data comes before samplerate.
  2. Step 2: Compare with given code

    The code passes samplerate before data, which is incorrect and will cause an error.
  3. Final Answer:

    Arguments order is incorrect; data should come before samplerate -> Option B
  4. Quick Check:

    Write args order = file, data, samplerate [OK]
Hint: Remember: write(file, data, samplerate) order [OK]
Common Mistakes:
  • Swapping data and samplerate arguments
  • Assuming file extension must be .mp3
  • Thinking missing NumPy import causes write error
5. You want to read an audio file, double its volume, and save it back. Which code snippet correctly does this using soundfile?
hard
A. sr, data = sf.read('input.wav') data = data * 2 sf.write('output.wav', sr, data)
B. data = sf.read('input.wav') data = data * 2 sf.write('output.wav', data)
C. data, sr = sf.read('input.wav') data = data * 2 sf.write('output.wav', data, sr)
D. data, sr = sf.read('input.wav') sf.write('output.wav', sr, data * 2)

Solution

  1. Step 1: Read audio correctly

    Use data, sr = sf.read('input.wav') to get data and sample rate.
  2. Step 2: Double the volume

    Multiply the data array by 2 to increase volume.
  3. Step 3: Write audio correctly

    Use sf.write('output.wav', data, sr) with data first, then sample rate.
  4. Final Answer:

    data, sr = sf.read('input.wav') data = data * 2 sf.write('output.wav', data, sr) -> Option C
  5. Quick Check:

    Read data, multiply, write data with sample rate [OK]
Hint: Read data, multiply, write with correct argument order [OK]
Common Mistakes:
  • Swapping data and sample rate in write
  • Not unpacking read return values
  • Forgetting to multiply data before writing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard