how to import an audio file ?

조회 수: 168 (최근 30일)
jérôme TAÏEB
jérôme TAÏEB 2018년 9월 29일
답변: Sukanya 2024년 12월 24일
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');

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by