How do I import an audio file into simulink?

조회 수: 48 (최근 30일)
Victor
Victor 2025년 11월 26일 21:57
댓글: Victor 2025년 11월 28일 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.

채택된 답변

Paul
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.
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
Walter Roberson
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.
Victor
Victor 2025년 11월 28일 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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Signal Import and Export에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by