How do I go about using startBackground to get live microphone data, and process that data live?

1 visualización (últimos 30 días)
Basically I want to get microphone data, put it through the B1 = bandpower(data,fs,freqrange1) method, and then output the result of the method. And I want it to do this constantly. How do I go about doing this? I've tried various documentation, but they don't explain well how to use the listener to get data into a variable. Here is what I have so far:
fs = 41000;
d = daq.getDevices;
dev = d(2);
s = daq.createSession('directsound');
ch = addAudioInputChannel(s, dev.ID, 1);
s.IsContinuous = true;
lh = addlistener(s,'DataAvailable', @(src,event) expr); //This is where I am getting stuck. How do I make this so it feeds the data it recieves from the mic into a variable called data that I can then use to input into the bandpower method.
freqrange1 = [20,60];
freqrange2 = [60,100];
freqrange3 = [100,500];
freqrange4 = [500,1000];
freqrange5 = [1000,1500];
freqrange6 = [1500,2000];
freqrange7 = [2000,2750];
data = startBackground(s);
while s.IsContinous
B1 = bandpower(data,fs,freqrange1);
B2 = bandpower(data,fs,freqrange2);
B3 = bandpower(data,fs,freqrange3);
B4 = bandpower(data,fs,freqrange4);
B5 = bandpower(data,fs,freqrange5);
B6 = bandpower(data,fs,freqrange6);
B7 = bandpower(data,fs,freqrange7);
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Mzo. de 2018
"How do I make this so it feeds the data it recieves from the mic into a variable called data that I can then use to input into the bandpower method."
You do not do that.
When you use background operations, have you to get rid of the idea that the background operation is going to return data that you can store into a variable and then proceed to analyze. Instead you need to make the function you designate as the listener do the work.
Look at the first example for startBackground:
s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod1','ai0','Voltage');
lh = addlistener(s,'DataAvailable',@plotData);
function plotData(src,event)
plot(event.TimeStamps,event.Data)
end
each time that data is available, the function plotData will be invoked with two arguments. The data is available in the Data field of the second argument.
For example you could code
function do_the_thing
[...]
lh = addlistener(s,'DataAvailable', @(src, event) analyze_bandpower(event.Data));
startBackground(s);
function analyze_bandpower(data) %nested so can use the shared variables freqrange*
B1 = bandpower(data,fs,freqrange1);
B2 = bandpower(data,fs,freqrange2);
B3 = bandpower(data,fs,freqrange3);
B4 = bandpower(data,fs,freqrange4);
B5 = bandpower(data,fs,freqrange5);
B6 = bandpower(data,fs,freqrange6);
B7 = bandpower(data,fs,freqrange7);
end
end
  3 comentarios
Walter Roberson
Walter Roberson el 1 de Mzo. de 2018
You would store it all in a file such as do_the_thing.m
Do not try to do this from the command line: the code for analyze_bandpower relies upon the freqrange* variables being shared variables. To be able to do this from the command line, you would need to pass the freqrange* variables to analyze_bandpower which you would have had to store into analyze_bandpower.m
John Hackett
John Hackett el 1 de Mzo. de 2018
I see. I think I have something now. At the very least the number get bigger when I speak. I ended up just putting the fs and freqrange variables into the function itself. For some reason even when I ran it as a file, it couldn't find the variables.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by