Trying to use audiorecorder for impulse response testing in a for loop
Hi everyone,
I’m trying to test the impulse response of a microphone that i have attached to a rotary device connected to an arduino board. Basically trying to automate getting the impulse response at different angles. Thing is, I can’t get the audiorecorder function to work properly within the for loop. It only gives me the impulse response for the final iteration. The rest has no values.
Goal is to play a white noise, record the data from mic using audiorecorder, do my ffts and iffts for impulse response, rotate by a certain angle and repeat the process.
My code is below. Any help would be greatly appreciated because I’m at a complete standstill on what to do here.
close all
clear all
% Parameters
fs = 44100; % Sampling frequency
dur = 30; % Duration of recording in seconds
dt = 1/fs; % Time step
% Create time vector for plotting
time = 0:dt:dur-dt;
% Generate white noise for the entire duration
noise=randn(dur*fs,1);
ramp_up = 0:0.01:1;
ramp_up=ramp_up’;
ramp_down= 1:-0.01:0;
ramp_down=ramp_down’;
noise(1:length(ramp_up))=noise(1:length(ramp_up)).*ramp_up;
noise(end-length(ramp_down)+1:end)=noise(end-length(ramp_down)+1:end).*ramp_down;
noise=noise./max(noise);
recObj = audiorecorder(fs, 24, 2, 2);
%Arduino Setup
clear dcm sm shield a;
a = arduino(‘COM5′,’Uno’,’Libraries’,’AdafruitMotorShieldV2′);
shield = addon(a,’AdafruitMotorShieldV2′);
addrs = scanI2CBus(a,0);
sm = stepper(shield,2,200);
sm.RPM = 3;
Angle = 45; %Rotation Angle
n_rot = 180 / Angle; %Number of Rotations
IM_L=1000; % Truncated impulse response length in number of data points
LS_IR=zeros(n_rot,IM_L);
nfft = 96000; % FFT size
df = fs / nfft; % Frequency resolution
faxis = df:df:nfft*df; % Frequency axis
nFreq=length(faxis);
%Initializing data storage
nSamples = fs * dur;
allData_mic = zeros(nSamples, n_rot);
allData_spk = zeros(nSamples,n_rot);
allIR = zeros(nFreq, n_rot);
allIR_trunc=zeros(IM_L, n_rot);
allfrf=zeros(nFreq/2,n_rot);
allfrf_trunc=zeros(IM_L,n_rot);
%for i = 1:1
for i = 1:n_rot+1
%recording
sound(noise, fs);
% Record two-channel microphone input (24-bit, 2 channels)
recordblocking(recObj, dur); % Record for specified duration
% Get recorded data (two channels)
recordedData = getaudiodata(recObj);
spkinput = recordedData(:, 1); % First channel: Speaker
microphoneInput = recordedData(:, 2); % Second channel: Microphone
microphoneInput = microphoneInput./0.0462;%Left Ear (Fixed)
%microphoneInput = microphoneInput./0.0630;%Right Ear
%% FFT
nav = floor(length(spkinput) / nfft) * 2 – 1; % Number of averages
% Initialize accumulators for power spectral densities and cross-spectral density
G1 = zeros(nfft, 1); % Power Spectral Density of Speaker
G2 = zeros(nfft, 1); % Power Spectral Density of Microphone
G12 = zeros(nfft, 1); % Cross-Spectral Density between Microphone 1 and Microphone 2
G21 = zeros(nfft, 1);
% Loop through the signal with 50% overlap and apply FFT
for i = 1:nav
p1 = 1 + nfft * (i – 1) * 0.5; % Start index for the current block
p2 = p1 + nfft-1; % End index for the current block
% Apply Hann window to the blocks
win=hann(length(spkinput(p1:p2))); % Create a Hann window for the current segment
% Applying windows to blocks
spk_win = win.*spkinput(p1:p2); % Mic1 input
mic_win = win.*microphoneInput(p1:p2); % Mic2 input
% FFT of the windowed blocks
Xf = fft(spk_win, nfft); % FFT of Mic1
Yf = fft(mic_win, nfft); % FFT of Mic2
G1 = G1 + Xf .* conj(Xf); % PSD of Speaker input
G2 = G2 + Yf .* conj(Yf); % PSD of Microphone recording
G12 = G12 + Xf .* conj(Yf); % CSD between Microphone and Speaker
G21=G21 + Yf.*conj(Xf);
end
G1 = 2*G1(1:nfft/2)./(nfft*nfft*nav);
G2 = 2*G2(1:nfft/2)./(nfft*nfft*nav);
G12 = 2*G12(1:nfft/2)./(nfft*nfft*nav);
G21 = 2*G21(1:nfft/2)./(nfft*nfft*nav);
% Compute Coherence Function
coherence = abs(G21).^2 ./ (G1 .* G2);
% Compute Frequency Response Function (FRF)
frf = G2./G12; % FRF
magdB=20*log10(abs(frf));
%% Impulse Response
IR = ifft(frf,nfft);
IR_trunc=IR(1:IM_L);
%impulse_time = (0:nfft-1) * dt;
% n_samp=length(frf);
% impulse_time= 0:dt:(length(microphoneInput1)-1)*dt;
%store everything to the two vectors made above
allData_spk(:, i) = spkinput;
allData_mic(:, i) = microphoneInput;
allIR (:,i) = IR;
allIR_trunc (:,i)= IR_trunc;
allfrf (:,i) = frf;
filenameOut = sprintf(‘HeadTesting_Baseline_27Jan_%ddeg.mat’, ((i-1)*5));
%Movement———–
% – moves it to the left (counterclockwise) (standing behind it)
% + moves it to the right (clockwise) (stangind behind it)
if i~=n_rot+1
move(sm, +50*Angle);
end
end
clear dcm sm shield a;Hi everyone,
I’m trying to test the impulse response of a microphone that i have attached to a rotary device connected to an arduino board. Basically trying to automate getting the impulse response at different angles. Thing is, I can’t get the audiorecorder function to work properly within the for loop. It only gives me the impulse response for the final iteration. The rest has no values.
Goal is to play a white noise, record the data from mic using audiorecorder, do my ffts and iffts for impulse response, rotate by a certain angle and repeat the process.
My code is below. Any help would be greatly appreciated because I’m at a complete standstill on what to do here.
close all
clear all
% Parameters
fs = 44100; % Sampling frequency
dur = 30; % Duration of recording in seconds
dt = 1/fs; % Time step
% Create time vector for plotting
time = 0:dt:dur-dt;
% Generate white noise for the entire duration
noise=randn(dur*fs,1);
ramp_up = 0:0.01:1;
ramp_up=ramp_up’;
ramp_down= 1:-0.01:0;
ramp_down=ramp_down’;
noise(1:length(ramp_up))=noise(1:length(ramp_up)).*ramp_up;
noise(end-length(ramp_down)+1:end)=noise(end-length(ramp_down)+1:end).*ramp_down;
noise=noise./max(noise);
recObj = audiorecorder(fs, 24, 2, 2);
%Arduino Setup
clear dcm sm shield a;
a = arduino(‘COM5′,’Uno’,’Libraries’,’AdafruitMotorShieldV2′);
shield = addon(a,’AdafruitMotorShieldV2′);
addrs = scanI2CBus(a,0);
sm = stepper(shield,2,200);
sm.RPM = 3;
Angle = 45; %Rotation Angle
n_rot = 180 / Angle; %Number of Rotations
IM_L=1000; % Truncated impulse response length in number of data points
LS_IR=zeros(n_rot,IM_L);
nfft = 96000; % FFT size
df = fs / nfft; % Frequency resolution
faxis = df:df:nfft*df; % Frequency axis
nFreq=length(faxis);
%Initializing data storage
nSamples = fs * dur;
allData_mic = zeros(nSamples, n_rot);
allData_spk = zeros(nSamples,n_rot);
allIR = zeros(nFreq, n_rot);
allIR_trunc=zeros(IM_L, n_rot);
allfrf=zeros(nFreq/2,n_rot);
allfrf_trunc=zeros(IM_L,n_rot);
%for i = 1:1
for i = 1:n_rot+1
%recording
sound(noise, fs);
% Record two-channel microphone input (24-bit, 2 channels)
recordblocking(recObj, dur); % Record for specified duration
% Get recorded data (two channels)
recordedData = getaudiodata(recObj);
spkinput = recordedData(:, 1); % First channel: Speaker
microphoneInput = recordedData(:, 2); % Second channel: Microphone
microphoneInput = microphoneInput./0.0462;%Left Ear (Fixed)
%microphoneInput = microphoneInput./0.0630;%Right Ear
%% FFT
nav = floor(length(spkinput) / nfft) * 2 – 1; % Number of averages
% Initialize accumulators for power spectral densities and cross-spectral density
G1 = zeros(nfft, 1); % Power Spectral Density of Speaker
G2 = zeros(nfft, 1); % Power Spectral Density of Microphone
G12 = zeros(nfft, 1); % Cross-Spectral Density between Microphone 1 and Microphone 2
G21 = zeros(nfft, 1);
% Loop through the signal with 50% overlap and apply FFT
for i = 1:nav
p1 = 1 + nfft * (i – 1) * 0.5; % Start index for the current block
p2 = p1 + nfft-1; % End index for the current block
% Apply Hann window to the blocks
win=hann(length(spkinput(p1:p2))); % Create a Hann window for the current segment
% Applying windows to blocks
spk_win = win.*spkinput(p1:p2); % Mic1 input
mic_win = win.*microphoneInput(p1:p2); % Mic2 input
% FFT of the windowed blocks
Xf = fft(spk_win, nfft); % FFT of Mic1
Yf = fft(mic_win, nfft); % FFT of Mic2
G1 = G1 + Xf .* conj(Xf); % PSD of Speaker input
G2 = G2 + Yf .* conj(Yf); % PSD of Microphone recording
G12 = G12 + Xf .* conj(Yf); % CSD between Microphone and Speaker
G21=G21 + Yf.*conj(Xf);
end
G1 = 2*G1(1:nfft/2)./(nfft*nfft*nav);
G2 = 2*G2(1:nfft/2)./(nfft*nfft*nav);
G12 = 2*G12(1:nfft/2)./(nfft*nfft*nav);
G21 = 2*G21(1:nfft/2)./(nfft*nfft*nav);
% Compute Coherence Function
coherence = abs(G21).^2 ./ (G1 .* G2);
% Compute Frequency Response Function (FRF)
frf = G2./G12; % FRF
magdB=20*log10(abs(frf));
%% Impulse Response
IR = ifft(frf,nfft);
IR_trunc=IR(1:IM_L);
%impulse_time = (0:nfft-1) * dt;
% n_samp=length(frf);
% impulse_time= 0:dt:(length(microphoneInput1)-1)*dt;
%store everything to the two vectors made above
allData_spk(:, i) = spkinput;
allData_mic(:, i) = microphoneInput;
allIR (:,i) = IR;
allIR_trunc (:,i)= IR_trunc;
allfrf (:,i) = frf;
filenameOut = sprintf(‘HeadTesting_Baseline_27Jan_%ddeg.mat’, ((i-1)*5));
%Movement———–
% – moves it to the left (counterclockwise) (standing behind it)
% + moves it to the right (clockwise) (stangind behind it)
if i~=n_rot+1
move(sm, +50*Angle);
end
end
clear dcm sm shield a; Hi everyone,
I’m trying to test the impulse response of a microphone that i have attached to a rotary device connected to an arduino board. Basically trying to automate getting the impulse response at different angles. Thing is, I can’t get the audiorecorder function to work properly within the for loop. It only gives me the impulse response for the final iteration. The rest has no values.
Goal is to play a white noise, record the data from mic using audiorecorder, do my ffts and iffts for impulse response, rotate by a certain angle and repeat the process.
My code is below. Any help would be greatly appreciated because I’m at a complete standstill on what to do here.
close all
clear all
% Parameters
fs = 44100; % Sampling frequency
dur = 30; % Duration of recording in seconds
dt = 1/fs; % Time step
% Create time vector for plotting
time = 0:dt:dur-dt;
% Generate white noise for the entire duration
noise=randn(dur*fs,1);
ramp_up = 0:0.01:1;
ramp_up=ramp_up’;
ramp_down= 1:-0.01:0;
ramp_down=ramp_down’;
noise(1:length(ramp_up))=noise(1:length(ramp_up)).*ramp_up;
noise(end-length(ramp_down)+1:end)=noise(end-length(ramp_down)+1:end).*ramp_down;
noise=noise./max(noise);
recObj = audiorecorder(fs, 24, 2, 2);
%Arduino Setup
clear dcm sm shield a;
a = arduino(‘COM5′,’Uno’,’Libraries’,’AdafruitMotorShieldV2′);
shield = addon(a,’AdafruitMotorShieldV2′);
addrs = scanI2CBus(a,0);
sm = stepper(shield,2,200);
sm.RPM = 3;
Angle = 45; %Rotation Angle
n_rot = 180 / Angle; %Number of Rotations
IM_L=1000; % Truncated impulse response length in number of data points
LS_IR=zeros(n_rot,IM_L);
nfft = 96000; % FFT size
df = fs / nfft; % Frequency resolution
faxis = df:df:nfft*df; % Frequency axis
nFreq=length(faxis);
%Initializing data storage
nSamples = fs * dur;
allData_mic = zeros(nSamples, n_rot);
allData_spk = zeros(nSamples,n_rot);
allIR = zeros(nFreq, n_rot);
allIR_trunc=zeros(IM_L, n_rot);
allfrf=zeros(nFreq/2,n_rot);
allfrf_trunc=zeros(IM_L,n_rot);
%for i = 1:1
for i = 1:n_rot+1
%recording
sound(noise, fs);
% Record two-channel microphone input (24-bit, 2 channels)
recordblocking(recObj, dur); % Record for specified duration
% Get recorded data (two channels)
recordedData = getaudiodata(recObj);
spkinput = recordedData(:, 1); % First channel: Speaker
microphoneInput = recordedData(:, 2); % Second channel: Microphone
microphoneInput = microphoneInput./0.0462;%Left Ear (Fixed)
%microphoneInput = microphoneInput./0.0630;%Right Ear
%% FFT
nav = floor(length(spkinput) / nfft) * 2 – 1; % Number of averages
% Initialize accumulators for power spectral densities and cross-spectral density
G1 = zeros(nfft, 1); % Power Spectral Density of Speaker
G2 = zeros(nfft, 1); % Power Spectral Density of Microphone
G12 = zeros(nfft, 1); % Cross-Spectral Density between Microphone 1 and Microphone 2
G21 = zeros(nfft, 1);
% Loop through the signal with 50% overlap and apply FFT
for i = 1:nav
p1 = 1 + nfft * (i – 1) * 0.5; % Start index for the current block
p2 = p1 + nfft-1; % End index for the current block
% Apply Hann window to the blocks
win=hann(length(spkinput(p1:p2))); % Create a Hann window for the current segment
% Applying windows to blocks
spk_win = win.*spkinput(p1:p2); % Mic1 input
mic_win = win.*microphoneInput(p1:p2); % Mic2 input
% FFT of the windowed blocks
Xf = fft(spk_win, nfft); % FFT of Mic1
Yf = fft(mic_win, nfft); % FFT of Mic2
G1 = G1 + Xf .* conj(Xf); % PSD of Speaker input
G2 = G2 + Yf .* conj(Yf); % PSD of Microphone recording
G12 = G12 + Xf .* conj(Yf); % CSD between Microphone and Speaker
G21=G21 + Yf.*conj(Xf);
end
G1 = 2*G1(1:nfft/2)./(nfft*nfft*nav);
G2 = 2*G2(1:nfft/2)./(nfft*nfft*nav);
G12 = 2*G12(1:nfft/2)./(nfft*nfft*nav);
G21 = 2*G21(1:nfft/2)./(nfft*nfft*nav);
% Compute Coherence Function
coherence = abs(G21).^2 ./ (G1 .* G2);
% Compute Frequency Response Function (FRF)
frf = G2./G12; % FRF
magdB=20*log10(abs(frf));
%% Impulse Response
IR = ifft(frf,nfft);
IR_trunc=IR(1:IM_L);
%impulse_time = (0:nfft-1) * dt;
% n_samp=length(frf);
% impulse_time= 0:dt:(length(microphoneInput1)-1)*dt;
%store everything to the two vectors made above
allData_spk(:, i) = spkinput;
allData_mic(:, i) = microphoneInput;
allIR (:,i) = IR;
allIR_trunc (:,i)= IR_trunc;
allfrf (:,i) = frf;
filenameOut = sprintf(‘HeadTesting_Baseline_27Jan_%ddeg.mat’, ((i-1)*5));
%Movement———–
% – moves it to the left (counterclockwise) (standing behind it)
% + moves it to the right (clockwise) (stangind behind it)
if i~=n_rot+1
move(sm, +50*Angle);
end
end
clear dcm sm shield a; audiorecorder, signal processing, audio, arduino, for loop MATLAB Answers — New Questions