MATLAB Answers

0

Recording sound while acquiring data from pressure sensor that connected with arduino

Asked by Dziban Naufal on 22 Aug 2019 at 4:46
Latest activity Commented on by Walter Roberson
on 28 Aug 2019 at 2:44
Hi,
I was trying to record sound using soundcard and pressure data from arduino simultaneously. I tried to use audiorecorder and recordblocking function in the same a while loop with pressure data acquisition command and i found that MATLAB will execute the recordblocking function till the set time limit then it continue to execute the acquisition command.
Is there any way to record two data (from soundcard and arduino) simultaneously?
Here are the codes that used:
a = arduino();
% Create a figure window to monitor the live data
fs=8000
nbits=8;
channels=1;
tmax = 10; % Total time for data collection in seconds
figure(1),
grid on,
xlabel ('Time (s)'), ylabel('Voltage');
axis([0 tmax+1 -0.5 5]);
recorder=audiorecorder(fs,nbits,channels);
% set initial index value, starting value
k = 0; %index
v = 0; %voltage
t = 0; %time
tic % Start timer
while toc <= tmax
k = k + 1;
t(k)=toc
disp('Start speaking')
recordblocking(recorder,sec);
disp('End of Recording');
v(k) = readVoltage(a,'A1');
t(k) = toc;
% Now plot the data
if k > 1
line([t(k-1) t(k)],[v(k-1) v(k)]);
drawnow;
end
end
for i=1:length(v)
vl(i)=v(i)-0.185728;
vt(i)=vl(i).*779.34;
end
plot(vt);
myRecording=getaudiodata(recorder);
audiowrite('test3.wav',myRecording,fs,'BitsPerSample',nbits);
[y,fs]=audioread('test3.wav');
figure;
plot(y);

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Walter Roberson
on 22 Aug 2019 at 5:08
Edited by Walter Roberson
on 28 Aug 2019 at 2:36

You would use record(), which will permit you to continue running. You would set up a recording duration ahead of time.
However, you are going to have a hard time synchronizing the inputs, unless you deliberately include some kind of synchronization signal on the readings.
You are also not going to be able to poll the arduino with readVoltage at more than 100 Hz or so -- a lot of people report success only down to about 40 Hz or so. In order to do better, you need to write code for the arduino that records a number of readings and sends them in batches as close to 1000 bytes long as you can manage. This is due to the fact that the connection is likely serial over USB, and USB polling rate is no more than 1000 transactions per second, with 1000 data bytes available in a serial packet (the other 4 bytes normally available are used by the serial-over-usb protocol to indicate the current status of the various likes such as DTR.)
When you need to synchronize channels, it is often advisable to use an Ni Compaq Daq chassis, especially configuring it to return sample timestamps.

  2 Comments

Thanks for the answer. I have tried to use record() function with the example that are provided by Matlab and the code seems like this :
s = serial('COM7');
fopen(s)
s.RecordDetail = 'verbose';
s.RecordName = 'MySerialFile.txt';
record(s,'on')
fprintf(s,'*IDN?')
out = fscanf(s);
record(s,'off')
fclose(s)
But when i tried to execute the code, the command window was showing alert like this:
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..
could you help me about this?
Thanks a lot.
record() would be applied to the audiorecorder session. Where you have
recordblocking(recorder,sec);
you would use
record(recorder,sec);
and then later when you knew enough time had elapsed you would getaudiodata(recorder)
Your stuff with fprintf(s,'*IDN?') would be primarily for use with Instrument Control Toolbox for GPIB or SCPI devices, perhaps with a VISA driver. It would never be used with a sound card, and it would rather rarely be used with an arduino

Sign in to comment.