How do I import an audio file into simulink?
Show older comments
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
More Answers (1)
Walter Roberson
on 27 Nov 2025
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.
Categories
Find more on Signal Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!