How do I import an audio file into simulink?

48 views (last 30 days)
Victor
Victor on 26 Nov 2025 at 21:57
Commented: Victor on 28 Nov 2025 at 22:02
I'm currently attempting to test a digital filter in simulink, but I do not know how to import my test audio into Simulink.
I recorded a .wav file on my computer and I need a way to add it to my simulation. From my understanding, simulink cannot read a wav file directly, so I need a way to convert it into a format that can be worked with. I repeatedly attempted to use the Playback block to load it but it did not find the .wav file in the directory despite it being my current folder.
In the simulation, it will have noise added to it, then pass through my filter which hopefully removes the noise. I used the filter design app in MATLAB and exported the filter to Simulink, so while it is not necessary for me to do it all in Simulink, it would be convenient.
I also need to be able to convert the output data back into a .wav file so I can listen to it again. I'm not sure if it is relevant, but the filter has a 8kHz sample rate.

Accepted Answer

Paul
Paul on 27 Nov 2025 at 21:17
Edited: Paul on 27 Nov 2025 at 23:51
Hi Victor,
It's not clear why it would be more convenient to do this in Simulink, but if that's really the case then perhaps the block From Multimedia File would be useful (assuming you have the requisite toolboxes). Also Audio Device Writer and To Multimedia File may be of interest.
But unless there's good reason to use Simulink, seems like it would be much easier to do this all in Matlab using: audioread and sound (or soundsc) with your filtering operation in between.
  2 Comments
Walter Roberson
Walter Roberson on 28 Nov 2025 at 20:24
From Multimedia File is a better solution than what I proposed. I searched, but I did not manage to come across that possibility.
Victor
Victor on 28 Nov 2025 at 22:02
In all honesty, I just wanted to use Simulink because the block diagram feels a little more intuitive due to how I was taught systems and signals, as I'm not much of a programmer. Still, I appreciate the options you gave me. I was able to locate the From Multimedia File block and I will see if I can finish the rest in Simulink

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 27 Nov 2025 at 2:01
As a pre-processing step in MATLAB, use audioread to read the audio into the workspace and fetch the sample rate. Then, if necessary, convert multiple channels to mono.
If you have the signal processing toolbox, use buffer to reshape the audio into blocks. If you do not have buffer, then you can calculate the number of data blocks (rounding up!) and if you did not already happen to end at the end of a block, write a 0 at location, and then reshape()
AudioBufferSize = 64; %adjust as needed
L = length(your_audio_data);
needed_L = ceil(L/AudioBufferSize) * AudioBufferSize;
if L ~= needed_L; your_audio_data(needed_L) = 0; end %fill with trailing zeros
buffered_data = reshape(your_audio_data, AudioBufferSize);
Or just
buffered_data = buffer(your_audio_data, AudioBufferSize); %if you have Signal Processing
Now construct marginal times:
marginal_times = (0:size(buffered_data,2)-1)*AudioBufferSize / Fs; %sampling frequency
and put them together:
labeled_audio = [marginal_times; buffered_data];
now write labeled_audio to a .mat file.
On the Simulink side, add a block to From File the .mat file you just saved.

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!