Guidance for importing a .wav audio file into MATLAB for digital filter simulation.

35 views (last 30 days)
Dear All,
I hope this message finds you well. I am currently working with Altera FPGA Cyclone IV and aim to implement a FIR filter for the ADC data originating from a microphone. Quartus provides an IP core (FIR II) for this purpose, and I intend to implement the coefficients generated with Matlab FilterDesigner.
More specifically I record for 2 seconds the sound from a microphone but when I choose to play the recording it has a lot of noise.
I would like to inquire if it's possible to import a .wav audio file into MATLAB for simulation and testing. I aim to fine-tune the filter and subsequently proceed to FPGA. Could you please guide me on the necessary steps to import the audio file, pass it through the digital filter, and observe the results in MATLAB?"
Thank you.
Best Regards

Accepted Answer

Hassaan
Hassaan on 23 Feb 2024
Step 1: Import the .wav Audio File
MATLAB's audioread function is perfect for importing audio files. You can use it to read your recorded .wav file as follows:
[signal, fs] = audioread('your_audio_file.wav');
  • signal contains the imported audio data.
  • fs is the sampling frequency of the audio file.
Step 2: Design Your FIR Filter
you can apply the filter to your audio signal using the filter function. If you have the Digital Signal Processing Toolbox, you might also explore designfilt and filtfilt for more advanced filter design and zero-phase filtering.
Assuming you have a set of coefficients named b (the numerator coefficients of the filter), you can apply your filter like this:
filtered_signal = filter(b, 1, signal);
Step 3: Play and Compare the Audio
After filtering, you might want to listen to the original and filtered audio to compare them. Use MATLAB's sound function to play back audio directly from MATLAB.
To play the original audio:
sound(signal, fs);
To play the filtered audio:
sound(filtered_signal, fs);
Step 4: Visualize the Results
Plotting the signals in the time domain or their frequency spectra can be very useful.
subplot(2,1,1);
plot(signal);
title('Original Audio Signal');
xlabel('Sample Number');
ylabel('Amplitude');
subplot(2,1,2);
plot(filtered_signal);
title('Filtered Audio Signal');
xlabel('Sample Number');
ylabel('Amplitude');
Frequency Spectrum:You can also visualize the frequency spectrum of your audio signal using the Fourier Transform.
L = length(signal); % Length of the signal
Y = fft(signal); % Fast Fourier Transform of the original signal
P2 = abs(Y/L); % Two-sided spectrum
P1 = P2(1:L/2+1); % Single-sided spectrum
P1(2:end-1) = 2*P1(2:end-1);
f = fs*(0:(L/2))/L;
plot(f, P1);
title('Single-Sided Amplitude Spectrum of Original Audio');
xlabel('Frequency (Hz)');
ylabel('|P1(f)|');
Do the same for filtered_signal to compare how the filter has altered the frequency content.
Step 5: Export the Processed Audio (Optional)
If you wish to save the filtered audio to a file, use audiowrite:
audiowrite('filtered_audio.wav', filtered_signal, fs);
Step 6: Implement on FPGA
After confirming that your filter works as intended in MATLAB:
  • Use the Filter Designer tool to export the filter coefficients.
  • Implement the FIR filter in your Altera FPGA Cyclone IV using the FIR II IP core, as you planned.
  • Load the coefficients into the FPGA and test with real-time data from the microphone.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation 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!