How do I import an audio file into simulink?
48 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Paul
2025-11-27,21:17
编辑:Paul
2025-11-27,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.
2 个评论
Walter Roberson
2025-11-28,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.
更多回答(1 个)
Walter Roberson
2025-11-27,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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!