Analogread on Matlab takes input 21 times per second. Very slow! ??

analog read takes input 21 times per second on Matlab instead of 10,000 times a second as mentioned on website. Following is the code:

clear, clc
a = arduino('COM6','due');
Fs=88000; % SAMPLING FREQUENCY = (2.2 X Fmax) X 2mics =88kHz
tic;
i=0;
while toc<2 %% 2 sec sample
i=i+1;
v1(i)= readVoltage(a, 0);
v2(i)= readVoltage(a, 7);
end

t1 = (0:length(v1)-1)/Fs;
t2 = (0:length(v2)-1)/Fs;
%----- Signal plot --------%
figure(1);
subplot(2,1,1)
plot(t1,v1,'r')
title('Left Mic')
xlabel('Time (s)')
subplot(2,1,2)
plot(t2,v2,'r')
title('Right Mic')
xlabel('Time (s)')

when entered >>i
i=42 i.e 41/2 = 21 times per second

Really?

In what way is this an Arduino programming question?

mtk93:
as mentioned on website.

What website?

...R

''It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.''

@ Nick
Someone told me that its a poorly designed function that sends a request and waits for a reply, suffering user-level Windows scheduling latency plus the tiny amount of time the A/D converter actually requires to make the measurement!. I thought it might be the appropriate place to discuss matlab programming but am sorry if its not the place.

Yes, but 104 µS per read doesn't translate into 21 reads per second. More like 9615 reads per second.

It's not poorly designed, it is designed to block, that's all. For beginners (towards who the Arduino is targeted) to handle non-blocking functions would be a big ask. So, they make them wait the 104 µS instead. And that is actually the time taken to make the measurement, it isn't extra on top of that.

You can write it in a non-blocking way (my page on interrupts shows how).

I'm guessing that the reduction to 21 reads per second is caused by Matlab.

...R

Maybe the baud rate. Unfortunately from that snippet we can't tell.

a = arduino('COM6','due');

Is this a Due question?

yes ::slight_smile: :relaxed:

I can't speak for how fast the Due analogRead takes. I'll move this to the Due section.

thnx