senox
senox

Reputation: 38

How to process multiple mics input Audio stream in matlab in real time

I need to get 3 input audio stream to matlab simultaneously using 3 USB mics and find out the highest amplitude in real time.

this is the mics initialization

mic1= dsp.AudioRecorder('DeviceName','ÇáãíßÑæÝæä (10- USB PnP Sound Device)', 'SampleRate', 48000, 'NumChannels', 1);
mic2= dsp.AudioRecorder('DeviceName','ÇáãíßÑæÝæä (9- USB PnP Sound Device)', 'SampleRate', 48000, 'NumChannels', 1);
mic3= dsp.AudioRecorder('DeviceName','ÇáãíßÑæÝæä (8- USB PnP Sound Device)', 'SampleRate', 48000, 'NumChannels', 1);



frame1=step(mic1);
frame2=step(mic2);
frame3=step(mic3);

what are the next steps?

Upvotes: 0

Views: 701

Answers (1)

rayryeng
rayryeng

Reputation: 104565

By examining your code, you have three audio signals with one channel each. If I understand what you want correctly, you want to find the highest sound made by any one signal overall over time. However, if I understand you correctly, you can't do this in real time as step for the AudioWriter can only capture one frame at a time. Because you're specifically trying to capture from all three audio devices, and the only way for you to capture audio is with step, you'll have to serially call step for each signal that you have.

Therefore, you'll need to capture all three audio signals separately, and then do your analysis. You'll also want to clip the sound after a certain point, so perhaps something like 5 seconds. Therefore, you'd do something like:

time_end = 5;

%// Capture audio signal 1
tic;    
frame1 = [];
while toc < time_end
  audio_in = step(mic1);
  frame1 = [frame1; audio_in(:)];
end

%// Capture audio signal 2
tic;    
frame2 = [];
while toc < time_end
  audio_in = step(mic2);
  frame2 = [frame2; audio_in(:)];
end


%// Capture audio signal 3
tic;    
frame3 = [];
while toc < time_end
  audio_in = step(mic3);
  frame3 = [frame3; audio_in(:)];
end

After this point, because the sounds will probably all be uneven length, you'll want to zero pad all of them so they all match the same length. After this, it's a matter of first finding the maximum amplitude for each sample for all three signals, and then finding the maximum out of all of this.

I'm not quite sure how the signals are shaped... if they are row or column vectors, so let's just make sure they're all column vectors. Then, use max and operate along the columns and find the maximum for each point in time, then find the maximum out of all of these.

Therefore:

%// Find lengths for all three signals
l1 = numel(frame1);
l2 = numel(frame2);
l3 = numel(frame3);
max_length = max([l1, l2, l3]);

%// Zero pad signals to make same length
frame1_pad = zeros(max_length,1);
frame2_pad = zeros(max_length,1);
frame2_pad = zeros(max_length,1);
frame1_pad(1:l1) = frame1;
frame2_pad(1:l2) = frame2;
frame3_pad(1:l3) = frame3;

%// Find maximum among each sample
max_signal = max([frame1_pad, frame2_pad, frame3_pad], [], 1);

%// Find the maximum amplitude overall and location
[max_amplitude, loc] = max(max_signal);

max_amplitude will contain the highest point overall at a particular time point for each of the three signals, and loc will tell you the location in the array of where it was found. If you want to find the actual time it occured, simply take loc and multiply by your sampling time (1/48000). Bear in mind that loc will be 1-indexed instead of 0-indexed, and so you need to subtract by 1 before multiplying by the sampling rate.

Therefore:

time_it_happened = (loc-1)*(1/48000);

time_it_happened will contain that time which the highest amplitude happened.

Good luck!

Upvotes: 1

Related Questions