Can someone please explain how this function? It is for simultaneously recording with the input(X) being white noise at FS 44100hz

2 views (last 30 days)
function recording = audioOI(x,fs)
% audioOI - Simulataneously plays and records the test signal.
% Uses Parallel Computing Toolbox.
% INPUT: x - test signal
% fs - sampling frequency
% OUTPUT: y - recorded signal
time = length(x)/fs; % play/record time
recObj = audiorecorder(44100,16,2);
parfor ii = 1:2
if ii==1
recordblocking(recObj, time);
recording{ii} = getaudiodata(recObj);
elseif ii==2
soundsc(x, fs);
end
end

Answers (2)

Hari
Hari on 18 Feb 2025
Edited: Walter Roberson on 19 Feb 2025
Hi,
I understand that you are looking for an explanation of the "audioOI" function, which is designed to simultaneously play and record a test signal, with the input signal being white noise at a sampling frequency of 44100 Hz.
Here is the explanation for the important steps:
Setup and Initialization:
The function calculates the duration of the signal based on its length and sampling frequency. It then initializes an "audiorecorder" object to record audio at 44100 Hz, 16-bit depth, and 2 channels (stereo).
time = length(x) / fs; % Calculate the duration of the signal
recObj = audiorecorder(44100, 16, 2); % Initialize the recorder
Parallel Execution:
The parfor loop is used to run two operations simultaneously. This requires the "Parallel Computing Toolbox".
parfor ii = 1:2
% Parallel loop for simultaneous operations
end
Recording Operation:
If the loop index ii is 1, the function records audio for the specified duration using recordblocking. The recorded audio data is then retrieved and stored in the recording cell array.
if ii == 1
recordblocking(recObj, time); % Record audio
recording{ii} = getaudiodata(recObj); % Retrieve audio data
Playback Operation:
If the loop index ii is 2, the function plays the input signal x using soundsc, which scales the audio for playback.
elseif ii == 2
soundsc(x, fs); % Play the input signal
end
Output:
The function returns the recorded signal stored in the recording variable. This allows you to analyze the recorded audio after playback and recording have completed.
Refer to the documentation of "audiorecorder": https://www.mathworks.com/help/matlab/ref/audiorecorder.html and "parfor": https://www.mathworks.com/help/parallel-computing/parfor.html for more details on these functions.
Hope this helps!

Walter Roberson
Walter Roberson on 19 Feb 2025
These days, since R2017a, the provided code should be replaced. Instead the provided code should use audioPlayerRecorder https://www.mathworks.com/help/audio/ref/audioplayerrecorder-system-object.html which provides streaming services for simultaneously playing a sound and recording input.

Categories

Find more on Large Files and Big Data 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!