how to import an audio file ?
조회 수: 168 (최근 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
댓글 수: 0
추가 답변 (1개)
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
% 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');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!