Bird
Raised Fist0
MATLABdata~10 mins

Audio signal processing in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Audio signal processing
Load Audio File
↓
Read Audio Data
↓
Apply Processing (e.g., filter, FFT)
↓
Analyze or Modify Signal
↓
Play or Save Processed Audio
↓
End
The flow starts by loading audio, then reading data, processing it, and finally playing or saving the result.
Execution Sample
MATLAB
audio = audioread('sound.wav');
filtered = lowpass(audio, 1000, 44100);
audioPlayer = audioplayer(filtered, 44100);
play(audioPlayer);
This code reads an audio file, applies a lowpass filter, and plays the filtered sound.
Execution Table
StepActionVariable StateResult/Output
1Read audio file 'sound.wav'audio = [audio samples array]Audio data loaded into 'audio'
2Apply lowpass filter at 1000 Hzfiltered = [filtered audio samples]Filtered audio stored in 'filtered'
3Create audio player objectaudioPlayer = audioplayer objectAudio player ready with filtered data
4Play audioaudioPlayer playingSound plays through speakers
5End of scriptNo changeProcessing complete
💡 Script ends after playing the filtered audio.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
audioempty[audio samples][audio samples][audio samples][audio samples][audio samples]
filteredemptyempty[filtered samples][filtered samples][filtered samples][filtered samples]
audioPlayeremptyemptyemptyaudioplayer objectplayingstopped after play
Key Moments - 2 Insights
Why do we need to specify the sample rate (44100) when filtering or playing audio?
The sample rate tells MATLAB how many samples per second the audio has, so it can correctly apply filters and play the sound at the right speed. See steps 2 and 3 in the execution_table.
What happens if the audio file is stereo but we apply processing assuming mono?
The processing might apply to both channels or cause errors. In the execution_table, 'audio' holds all channels, so you must handle each channel properly before filtering.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what variable holds the filtered audio after step 2?
Aaudio
Bfiltered
CaudioPlayer
Dsound
💡 Hint
Check the 'Variable State' column at step 2 in the execution_table.
At which step does the audio start playing?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Action' and 'Result/Output' columns in the execution_table.
If the sample rate was changed to 22050 in step 3, what would happen?
AAudio plays half as fast
BAudio plays twice as fast
CNo change in playback speed
DAudio file would not load
💡 Hint
Sample rate affects playback speed; see variable_tracker for 'audioPlayer' sample rate.
Concept Snapshot
Audio signal processing in MATLAB:
- Use audioread() to load audio data.
- Apply filters like lowpass() with sample rate.
- Use audioplayer() to play audio.
- Sample rate controls speed and quality.
- Process data arrays representing sound samples.
Full Transcript
This example shows how MATLAB processes audio step-by-step. First, it loads an audio file into a variable called 'audio'. Then it applies a lowpass filter to remove high frequencies, storing the result in 'filtered'. Next, it creates an audio player object with the filtered data and sample rate. Finally, it plays the sound through speakers. Variables change as the script runs, and the sample rate is important for correct playback speed. Beginners often wonder why sample rate is needed and how stereo audio is handled. The execution table and variable tracker clearly show each step and variable state.

Practice

(1/5)
1. What does the MATLAB function audioread do in audio signal processing?
easy
A. It saves audio data to a file.
B. It plays the audio sound through speakers.
C. It reads an audio file and loads the sound data into MATLAB.
D. It filters the audio signal to remove noise.

Solution

  1. Step 1: Understand the purpose of audioread

    The function audioread is used to load audio data from a file into MATLAB as a vector or matrix.
  2. Step 2: Differentiate from other audio functions

    Functions like sound play audio, audiowrite saves audio, and filter processes signals. audioread only reads data.
  3. Final Answer:

    It reads an audio file and loads the sound data into MATLAB. -> Option C
  4. Quick Check:

    audioread = read audio data [OK]
Hint: Remember: 'read' means load data, not play or save [OK]
Common Mistakes:
  • Confusing audioread with sound
  • Thinking it saves audio instead of loading
  • Assuming it filters or changes the audio
2. Which of the following is the correct syntax to apply a simple low-pass filter b, a to an audio signal x in MATLAB?
easy
A. y = filter(b, a, x);
B. y = filter(x, b, a);
C. y = filter(a, b, x);
D. y = filter(x, a, b);

Solution

  1. Step 1: Recall the filter function syntax

    The MATLAB filter function syntax is y = filter(b, a, x); where b and a are filter coefficients and x is the input signal.
  2. Step 2: Identify correct argument order

    Options A, C, and D have the arguments in wrong order, which will cause errors or wrong output.
  3. Final Answer:

    y = filter(b, a, x); -> Option A
  4. Quick Check:

    filter(b, a, x) = correct syntax [OK]
Hint: Filter syntax is always filter(b, a, x) [OK]
Common Mistakes:
  • Swapping filter coefficients order
  • Putting signal before coefficients
  • Using wrong variable names
3. What will be the output size of y after running this MATLAB code?
x = randn(44100,1); % 1 second noise at 44.1kHz
b = [1 0 0.5]; a = 1;
y = filter(b, a, x);
medium
A. An error due to filter coefficients
B. A vector of size 44102x1
C. A scalar value
D. A vector of size 44100x1

Solution

  1. Step 1: Understand input and filter output length

    The input x is a 44100x1 vector. The filter function outputs a vector of the same length as input.
  2. Step 2: Check filter coefficients validity

    Coefficients b and a are valid. No error occurs. Output y matches input size.
  3. Final Answer:

    A vector of size 44100x1 -> Option D
  4. Quick Check:

    filter output length = input length [OK]
Hint: Filter output length equals input length [OK]
Common Mistakes:
  • Assuming output length changes with filter order
  • Expecting error from coefficients
  • Confusing vector with scalar output
4. Identify the error in this MATLAB code that tries to add an echo effect to an audio signal x:
delay = 5000;
echoGain = 0.6;
y = x;
y(delay:end) = y(delay:end) + echoGain * x(1:end-delay);
medium
A. The length of y(delay:end) and x(1:end-delay) do not match.
B. The variable echoGain should be greater than 1.
C. The indexing delay:end is invalid for vector y.
D. The echo effect requires a filter function, not direct addition.

Solution

  1. Step 1: Check vector lengths in addition

    The segment y(delay:end) has length length(y) - delay + 1. The segment x(1:end-delay) has length length(x) - delay. They differ by 1, causing a dimension mismatch error.
  2. Step 2: Eliminate other options

    A: Indexing is valid. B: echoGain=0.6 <1 is appropriate for decaying echo. D: Direct indexing addition works for simple echo.
  3. Final Answer:

    The length of y(delay:end) and x(1:end-delay) do not match. -> Option A
  4. Quick Check:

    len(y(delay:end))=N-d+1 vs len(x(1:end-d))=N-d [OK]
Hint: Check lengths of vectors before adding [OK]
Common Mistakes:
  • Assuming echoGain must be >1
  • Thinking filter is required for echo
  • Ignoring vector length mismatch
5. You want to create a MATLAB function that adds an echo effect to an audio signal x sampled at Fs. The echo delay is 0.3 seconds and echo gain is 0.5. Which code correctly implements this?
hard
A. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = x; y(delaySamples+1:end) = y(delaySamples+1:end) + 0.5*x(1:end-delaySamples); end
B. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = filter([1 zeros(1,delaySamples-1) 0.5], 1, x); end
C. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = x + 0.5 * [zeros(delaySamples,1); x]; end
D. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = zeros(length(x)+delaySamples,1); y(1:length(x)) = x; y(delaySamples+1:end) = y(delaySamples+1:end) + 0.5*x; end

Solution

  1. Step 1: Understand echo effect implementation

    Echo is created by adding a delayed and scaled copy of the signal to itself. Using filter with coefficients [1 zeros(1,delaySamples-1) 0.5] adds original plus delayed echo.
  2. Step 2: Evaluate each option

    function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = zeros(length(x)+delaySamples,1); y(1:length(x)) = x; y(delaySamples+1:end) = y(delaySamples+1:end) + 0.5*x; end extends length but adds full x without delay in echo part. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = x; y(delaySamples+1:end) = y(delaySamples+1:end) + 0.5*x(1:end-delaySamples); end correctly indexes x to match lengths. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = x + 0.5 * [zeros(delaySamples,1); x]; end adds vectors of different lengths causing error. function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = filter([1 zeros(1,delaySamples-1) 0.5], 1, x); end correctly uses filter with proper coefficients.
  3. Final Answer:

    function y = addEcho(x, Fs) delaySamples = round(0.3 * Fs); y = filter([1 zeros(1,delaySamples-1) 0.5], 1, x); end -> Option B
  4. Quick Check:

    Use filter with delayed coefficients for echo [OK]
Hint: Use filter with delayed coefficients for echo [OK]
Common Mistakes:
  • Adding vectors of different lengths directly
  • Forgetting to delay the echo signal
  • Incorrect indexing causing errors
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