Real Time Conversion from Sample to Frequency and Time
13 views (last 30 days)
Show older comments
I'm running a real time audio processing softwar in Matlab where the input audio is graphed real time, as well as its real fft and ifft. I'm running into the issue of converting the samples to time or frequency.
Here's my code:
recorder = audiorecorder(96000, 24, 1);
disp('Start speaking.')
recorder.record(10);
while recorder.isrecording()
pause(0.1);
subplot(3,1,1);
plot(recorder.getaudiodata());
title('Orginal Recording');
xlabel('Time(Seconds)')
ylabel('Amplitude')
subplot(3,1,2);
x=fftshift(fft(recorder.getaudiodata()));
plot(real(x));
title('FFT Shift');
subplot(3,1,3);
y=ifftshift(x);
plot(real(y));
title('Inverse FFT Shift');
end
disp('End of Recording.');
I'm told to use something like Fs and samples and I don't understand it. I'm still quite new to Matlab.
0 Comments
Answers (1)
Deepak
on 6 Dec 2024
To resolve the issue of converting audio samples to time or frequency in real-time audio processing, we can use the sampling frequency (Fs) to construct appropriate time and frequency axes.
For time, create a vector “t” by dividing sample indices by “Fs”, which converts indices to seconds. For frequency, use the FFT to transform the audio data and create a frequency vector “f” using “f = (-numSamples/2:numSamples/2-1) * (Fs / numSamples);” to map FFT indices to frequency in Hertz.
Ensure that the audio data is retrieved after recording is complete by using “recordblocking” to synchronize the recording process, preventing the "Recorder is empty" error and allowing for accurate plotting of both time-domain and frequency-domain representations.
Below is the MATLAB code to achieve the same:
recorder = audiorecorder(96000, 24, 1);
Fs = 96000; % Sampling frequency
disp('Start speaking.');
% Use recordblocking to ensure the recording is captured
recordblocking(recorder, 10);
% Retrieve audio data after recording is complete
audioData = recorder.getaudiodata();
numSamples = length(audioData);
% Save the original audio data to a WAV file
audiowrite('original_recording.wav', audioData, Fs);
% Time vector for the original recording
t = (0:numSamples-1) / Fs;
subplot(3,1,1);
plot(t, audioData);
title('Original Recording');
xlabel('Time (Seconds)');
ylabel('Amplitude');
% FFT and frequency vector
x = fftshift(fft(audioData));
f = (-numSamples/2:numSamples/2-1) * (Fs / numSamples);
subplot(3,1,2);
plot(f, abs(x));
title('FFT Shift');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% IFFT
y = ifftshift(x);
subplot(3,1,3);
plot(t, real(ifft(y)));
title('Inverse FFT Shift');
xlabel('Time (Seconds)');
ylabel('Amplitude');
disp('End of Recording.');
Please find attached the documentation of functions used for reference:
audiorecorder: www.mathworks.com/help/matlab/ref/audiorecorder.html
I hope this will help in resolving the issue.
0 Comments
See Also
Categories
Find more on Transforms 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!