how to import an audio file ?

170 次查看(过去 30 天)
Hi,
I would like to write that in the command windows:
load handel.mat
filename = 'handel.mp3';
audiowrite(filename,y,Fs);
clear y Fs
[y,Fs] = audioread('handel.mp3');
but i don't know to import my audio file 'handel.mp3' from my hard disk to Matlab.
Can you help me?
thanks

采纳的回答

Stephen23
Stephen23 2018-9-29
编辑:Stephen23 2018-9-29
Use audioread. Why do you think that your code is not working? What do you expect to happen?
  2 个评论
jérôme TAÏEB
jérôme TAÏEB 2018-9-29
移动:DGM 2024-5-21
the problem is the path of the audio file.
Does Matlab recognize the path of the audio file on the hard disk?
for example,in the following line:
[y,Fs] = audioread('handel.mp3')
if path of 'handel.mp3' is for example C:/Users/chemin1/chemin2,must i write:
[y,Fs] = audioread('C:/Users/chemin1/chemin2/handel.mp3')?
Marcus Niklasson
Marcus Niklasson 2021-12-7
移动:DGM 2024-5-21
audioread("C:/Users/chemin1/chemin2/handel.mp3")

请先登录,再进行评论。

更多回答(1 个)

Sukanya
Sukanya 2024-12-24
% Check if the file exists
if exist('C:\Users\sukan\Downloads\POCORINGTONE.wav', 'file') ~= 2
error('Audio file not found! Place the file in the current folder or specify the correct path.');
end
Audio file not found! Place the file in the current folder or specify the correct path.
% Load the audio file
[audio, fs] = audioread('C:\Users\sukan\Downloads\POCORINGTONE.wav'); % Replace with full path if needed
% Ensure the audio signal is a column vector
audio = audio(:, 1); % Use only the first channel if stereo
% Play the original audio
sound(audio, fs);
disp('Playing the original audio...');
pause(length(audio) / fs); % Wait for the audio to finish
% Resample the audio to match the filter's sampling frequency (100 Hz)
audio_resampled = resample(audio, 100, fs);
fs = 100; % Update the sampling frequency to 100 Hz
% IIR Filter Design
[b_iir, a_iir] = butter(4, 4/(fs/2), 'low');
% FIR Filter Design
numtaps = 50; % Filter order + 1
b_fir = fir1(numtaps-1, 4/(fs/2), 'low');
% Apply Filters to the Resampled Audio
filtered_audio_iir = filter(b_iir, a_iir, audio_resampled);
filtered_audio_fir = filter(b_fir, 1, audio_resampled);
% Play the Filtered Audio Signals
% IIR Filtered Audio
sound(filtered_audio_iir, fs);
disp('Playing the IIR filtered audio...');
pause(length(filtered_audio_iir) / fs);
% FIR Filtered Audio
sound(filtered_audio_fir, fs);
disp('Playing the FIR filtered audio...');
pause(length(filtered_audio_fir) / fs);
% Save the Filtered Audio as Files
audiowrite('filtered_audio_iir.wav', filtered_audio_iir, fs);
audiowrite('filtered_audio_fir.wav', filtered_audio_fir, fs);
disp('Filtered audio files saved: filtered_audio_iir.wav and filtered_audio_fir.wav');
% Time Vector for Plotting
t = (0:length(audio_resampled)-1)/fs;
% Plot the Signals in Time Domain
figure;
subplot(3, 1, 1);
plot(t, audio_resampled);
title('Original Audio Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 1, 2);
plot(t, filtered_audio_iir);
title('IIR Filtered Audio Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 1, 3);
plot(t, filtered_audio_fir);
title('FIR Filtered Audio Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Analyze Frequency Spectrum
N = length(audio_resampled);
freq = (0:N-1)*(fs/N); % Frequency vector
% FFT for Frequency Analysis
fft_audio = abs(fft(audio_resampled))/N;
fft_audio_iir = abs(fft(filtered_audio_iir))/N;
fft_audio_fir = abs(fft(filtered_audio_fir))/N;
% Plot Frequency Spectra
figure;
plot(freq, fft_audio, 'k', 'DisplayName', 'Original'); hold on;
plot(freq, fft_audio_iir, 'r', 'DisplayName', 'IIR Filtered');
plot(freq, fft_audio_fir, 'b', 'DisplayName', 'FIR Filtered');
xlim([0 50]); % Focus on 0-50 Hz
legend;
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Frequency Spectrum Comparison');

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by