Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: continuous acquisition

Subject: continuous acquisition

From: Davide Borelli

Date: 5 Dec, 2007 11:05:38

Message: 1 of 19

Hello
I acquire with a NI PCI-4454 board and I need to acquire
data with high frequency (above 20kHz) for several
minutes; I would also like to plot the data (at least one
per second) while acquiring them.
Is it possible?
At first I tried with this cicle:

for i=1:NumberOfMeasures
    tic
    start(ai);
    trigger(ai);
    data=getdata(ai);
    MeanValue(i)=mean(data);
    plot(MeanValue);
    t=toc;
end

The problem is that the toc time is about 0.3 seconds,
that is too much to acquire fast-changing potentials...
but if I use a unique acquisition I cannot make it last
more than few minutes, because the number of data acquired
is too big!
Is there a solution?

Than you
Davide B.

Subject: continuous acquisition

From: Davide Borelli

Date: 5 Dec, 2007 11:07:40

Message: 2 of 19

Sorry I posted the same message 5 times, but it seemed the
connection didn't work!
Davide B.

"Davide Borelli" <davidepub@microonda.net> wrote in
message <fj60m2$p81$1@fred.mathworks.com>...
> Hello
> I acquire with a NI PCI-4454 board and I need to acquire
> data with high frequency (above 20kHz) for several
> minutes; I would also like to plot the data (at least
one
> per second) while acquiring them.
> Is it possible?
> At first I tried with this cicle:
>
> for i=1:NumberOfMeasures
> tic
> start(ai);
> trigger(ai);
> data=getdata(ai);
> MeanValue(i)=mean(data);
> plot(MeanValue);
> t=toc;
> end
>
> The problem is that the toc time is about 0.3 seconds,
> that is too much to acquire fast-changing potentials...
> but if I use a unique acquisition I cannot make it last
> more than few minutes, because the number of data
acquired
> is too big!
> Is there a solution?
>
> Than you
> Davide B.

Subject: continuous acquisition

From: Steve Amphlett

Date: 5 Dec, 2007 11:13:30

Message: 3 of 19

"Davide Borelli" <davidepub@microonda.net> wrote in message
<fj60ps$qi6$1@fred.mathworks.com>...
> Sorry I posted the same message 5 times, but it seemed
the
> connection didn't work!
> Davide B.

I counted nine postings.

Subject: continuous acquisition

From: Davide Borelli

Date: 5 Dec, 2007 11:29:07

Message: 4 of 19

Yes, sorry again, I did not do it on purpose.
Davide B.

"Davide Borelli" <davidepub@microonda.net> wrote in
message <fj60m2$p81$1@fred.mathworks.com>...
> Hello
> I acquire with a NI PCI-4454 board and I need to acquire
> data with high frequency (above 20kHz) for several
> minutes; I would also like to plot the data (at least
one
> per second) while acquiring them.
> Is it possible?
> At first I tried with this cicle:
>
> for i=1:NumberOfMeasures
> tic
> start(ai);
> trigger(ai);
> data=getdata(ai);
> MeanValue(i)=mean(data);
> plot(MeanValue);
> t=toc;
> end
>
> The problem is that the toc time is about 0.3 seconds,
> that is too much to acquire fast-changing potentials...
> but if I use a unique acquisition I cannot make it last
> more than few minutes, because the number of data
acquired
> is too big!
> Is there a solution?
>
> Than you
> Davide B.

Subject: continuous acquisition

From: Steve Amphlett

Date: 5 Dec, 2007 12:08:39

Message: 5 of 19

"Davide Borelli" <davidepub@microonda.net> wrote in message
<fj6223$af1$1@fred.mathworks.com>...
> Yes, sorry again, I did not do it on purpose.
> Davide B.

Sorry for the jibe. Only joking, honestly.

> > for i=1:NumberOfMeasures
> > tic
> > start(ai);
> > trigger(ai);
> > data=getdata(ai);
> > MeanValue(i)=mean(data);
> > plot(MeanValue);
> > t=toc;
> > end

There are a few things you can do here to decrease your
cycle time:

- Pre-allocate your MeanValue array (zeros, NaNs,
whatever). This will have a huge effect on speed and will
prevent your loop from slowing down over time.

- Set up your plot outside of the loop and store a handle
to it: h=plot(MeanValue); Now modify the handle's ydata in
the loop rather than replotting. I don't have ML today so
can't test it - you may need to set the erasemode and/or
use drawnow to refresh the plot.

Subject: continuous acquisition

From: Lars Warmbold

Date: 5 Dec, 2007 12:42:57

Message: 6 of 19

%% Create TWO *.m-files from this functions

% first *.m-file
function plotaqcuiredata

N = 1024;
SampRate= 44100;

%% Create Plot
figure(1)
hPlot = plot(zeros(1,N));
axis([0 N -2 2]);
grid on;

%% create 'LineIn'
ai = analoginput('winsound');
chan = addchannel(ai,1);

handles.N = N;
handles.ai = ai;
handles.hPlot = hPlot;

%% config LineIn
set(ai,'SampleRate' , SampRate);
set(ai,'SamplesPerTrigger' , inf);
set(ai,'SamplesAcquiredFcnCount', N);
set(ai,'SamplesAcquiredFcn' , {@update_plot, handles});


start(ai);
pause(5);
stop(ai);




% second *.m-file
function update_plot(dummy1, dummy2, handles)
    
%% get aqcuired data from ai-Object
data = getdata(handles.ai,handles.N);
%% try also
% data = peekdata(handles.ai,handles.N);

%% plot data
set(handles.hPlot,'YData',data);




%% ---------- End --%

% now call plotaqcuiredata from command window

LARS

Subject: continuous acquisition

From: Lars Warmbold

Date: 5 Dec, 2007 14:34:56

Message: 7 of 19

"Lars Warmbold" <mustermann.klaus.TO.REMOVE@gmx.de> wrote in
message <fj66ch$sgs$1@fred.mathworks.com>...
> %% Create TWO *.m-files from this functions
>
> % first *.m-file
> function plotaqcuiredata
>
> N = 1024;
> SampRate= 44100;
>
> %% Create Plot
> figure(1)
> hPlot = plot(zeros(1,N));
> axis([0 N -2 2]);
> grid on;
>
> %% create 'LineIn'
> ai = analoginput('winsound');
> chan = addchannel(ai,1);
>
> handles.N = N;
> handles.ai = ai;
> handles.hPlot = hPlot;
>
> %% config LineIn
> set(ai,'SampleRate' , SampRate);
> set(ai,'SamplesPerTrigger' , inf);
> set(ai,'SamplesAcquiredFcnCount', N);
> set(ai,'SamplesAcquiredFcn' , {@update_plot, handles});
>
>
> start(ai);
> pause(5);
> stop(ai);
>
%% insert here:
delete(ai);


Subject: continuous acquisition

From: Davide Borelli

Date: 6 Dec, 2007 17:57:41

Message: 8 of 19

Ok, thank you to everyone, I don't know Matlab very well,
so it will keep me some days to test your suggestion.

I have a question: what is the difference between this
type of data acquisition (the one I use in my m-file) and
the audio acquisition with a soundcard? I mean: with a
normal soundcard and a common audio acquisition program I
can acquire data at 44kHz for several minutes without
stopping, and plotting them in real time; is it possible
to do that with my pci4454?
Thank you.
Davide B.

Subject: continuous acquisition

From: Lars Warmbold

Date: 7 Dec, 2007 08:40:42

Message: 9 of 19

If the hardware is supported by the daq-toolbox yes. Use
provided code and replace

%% create 'LineIn'
> ai = analoginput('winsound');
> chan = addchannel(ai,1);

by something like

%% create 'LineIn'
> ai = analoginput('nidaq', 1);
> chan = addchannel(ai,0);

See also

>> help daq

To see if Matlab recognizes your hardware you can check out

>> hw = daqhwinfo;
>> hw.installedadapters

Note that you may need the traditional NIdaq-driver.

If you like infinite length of aqcuisition it is very
usefull to implement your programm as a GUI.

>> help guide

LARS

Subject: continuous acquisition

From: Davide Borelli

Date: 7 Dec, 2007 11:29:32

Message: 10 of 19

Yes, I already use the acquisition toolbox to acquire with
Matlab.
My question was different: which kind of code should I use
to make a continuous acquisition like the one you make
with a soundcard? I would like to see the structure of a
program designed for a continuous acquisition like that,
because I have a m-file which doesn't allow to make
continuous acquisition, but several very short (and
possibly fast) acquisition.
Davide B.



"Lars Warmbold" <mustermann.klaus.TO.REMOVE@gmx.de> wrote
in message <fjb0ua$cfc$1@fred.mathworks.com>...
> If the hardware is supported by the daq-toolbox yes. Use
> provided code and replace
>
> %% create 'LineIn'
> > ai = analoginput('winsound');
> > chan = addchannel(ai,1);
>
> by something like
>
> %% create 'LineIn'
> > ai = analoginput('nidaq', 1);
> > chan = addchannel(ai,0);
>
> See also
>
> >> help daq
>
> To see if Matlab recognizes your hardware you can check
out
>
> >> hw = daqhwinfo;
> >> hw.installedadapters
>
> Note that you may need the traditional NIdaq-driver.
>
> If you like infinite length of aqcuisition it is very
> usefull to implement your programm as a GUI.
>
> >> help guide
>
> LARS

Subject: continuous acquisition

From: Lars Warmbold

Date: 7 Dec, 2007 13:12:39

Message: 11 of 19

"Davide Borelli" <davidepub@microonda.net> wrote in message
<fjbaqs$mu0$1@fred.mathworks.com>...
> Yes, I already use the acquisition toolbox to acquire with
> Matlab.
> My question was different: which kind of code should I use
> to make a continuous acquisition like the one you make
> with a soundcard? I would like to see the structure of a
> program designed for a continuous acquisition like that,
> because I have a m-file which doesn't allow to make
> continuous acquisition, but several very short (and
> possibly fast) acquisition.
> Davide B.
>

I don't understand your Problem, cause the provided code
does exactly what you want. Just replace

>> pause(5)
>> stop(ai)

by some other control (i.E. stop button in GUI) or remove them.

Once you started 'ai' it runs infinitely. You can also type

>> stop(ai)

into the command window when aqcuisition is done.

The code I provided plots the data and deletes them. If you
want to store them, you should look after the log to disk
mode of analoginput.
LARS

Subject: continuous acquisition

From: Davide Borelli

Date: 7 Dec, 2007 16:09:47

Message: 12 of 19

Ok, thank you, I had not read carefully your previous
answer to my post. I will try with the two separate .m
file (just a curiosity: what is the difference between
making two different files and putting that in two
different functions in the same file?).
Thank you very much
Davide B.


"Lars Warmbold" <mustermann.klaus.TO.REMOVE@gmx.de> wrote
in message <fjbgs7$ggp$1@fred.mathworks.com>...
> "Davide Borelli" <davidepub@microonda.net> wrote in
message
> <fjbaqs$mu0$1@fred.mathworks.com>...
> > Yes, I already use the acquisition toolbox to acquire
with
> > Matlab.
> > My question was different: which kind of code should I
use
> > to make a continuous acquisition like the one you make
> > with a soundcard? I would like to see the structure of
a
> > program designed for a continuous acquisition like
that,
> > because I have a m-file which doesn't allow to make
> > continuous acquisition, but several very short (and
> > possibly fast) acquisition.
> > Davide B.
> >
>
> I don't understand your Problem, cause the provided code
> does exactly what you want. Just replace
>
> >> pause(5)
> >> stop(ai)
>
> by some other control (i.E. stop button in GUI) or
remove them.
>
> Once you started 'ai' it runs infinitely. You can also
type
>
> >> stop(ai)
>
> into the command window when aqcuisition is done.
>
> The code I provided plots the data and deletes them. If
you
> want to store them, you should look after the log to disk
> mode of analoginput.
> LARS

Subject: continuous acquisition

From: Davide Borelli

Date: 11 Dec, 2007 15:27:43

Message: 13 of 19

I made it!
Now I have another problem: I want to acquire 100 samples
(at 50000 samples per second) and plot the mean values.
I tried this code for the update_plot.m file:


function update_plot(dummy1,dummy2,handles)
% GET ACQUIRED DATA FROM ai-OBJECT
data=getdata(handles.ai,handles.SamplesNumber);
Media=mean(data);
handles.Volt(:)=Media;

%PLOT DATA IN THE GUIDE WINDOW
axes(handles.PlotChannel);
plot(handles.Volt);


but every time I run the program I see handles.Volt is not
a vector with increasing dimension but a 1x1 vector, i.e.
a scalar. How can I do to make the update_plot.m file to
keep de data in an array, in order to save it in a file at
the end of the measure?

Thank you
Davide B.





"Davide Borelli" <davidepub@microonda.net> wrote in
message <fjbr8b$qi7$1@fred.mathworks.com>...
> Ok, thank you, I had not read carefully your previous
> answer to my post. I will try with the two separate .m
> file (just a curiosity: what is the difference between
> making two different files and putting that in two
> different functions in the same file?).
> Thank you very much
> Davide B.
>
>
> "Lars Warmbold" <mustermann.klaus.TO.REMOVE@gmx.de>
wrote
> in message <fjbgs7$ggp$1@fred.mathworks.com>...
> > "Davide Borelli" <davidepub@microonda.net> wrote in
> message
> > <fjbaqs$mu0$1@fred.mathworks.com>...
> > > Yes, I already use the acquisition toolbox to
acquire
> with
> > > Matlab.
> > > My question was different: which kind of code should
I
> use
> > > to make a continuous acquisition like the one you
make
> > > with a soundcard? I would like to see the structure
of
> a
> > > program designed for a continuous acquisition like
> that,
> > > because I have a m-file which doesn't allow to make
> > > continuous acquisition, but several very short (and
> > > possibly fast) acquisition.
> > > Davide B.
> > >
> >
> > I don't understand your Problem, cause the provided
code
> > does exactly what you want. Just replace
> >
> > >> pause(5)
> > >> stop(ai)
> >
> > by some other control (i.E. stop button in GUI) or
> remove them.
> >
> > Once you started 'ai' it runs infinitely. You can also
> type
> >
> > >> stop(ai)
> >
> > into the command window when aqcuisition is done.
> >
> > The code I provided plots the data and deletes them.
If
> you
> > want to store them, you should look after the log to
disk
> > mode of analoginput.
> > LARS
>

Subject: continuous acquisition

From: Lars

Date: 12 Dec, 2007 20:04:39

Message: 14 of 19

say your data is

>> data = [1 2 3 4 5];

then

>> mean(a) = 3 (!!!)

now lets say

>> a = [1 2 3 4 5; 6 7 8 9 10]

then

>> mean(a) = 3.5 4.5 5.5 6.5 7.5

You need more than one set of samples.
Create an array lets say handles.myarray = zeros(10,
length_N) and a counter (say handles.counter=1) in the
openingFcn.

now you can compute insight the update_plot - routine
(It works like a register...) the following:

% write fresh data in selected row of myarray
handles.myarray(handles.counter, :)=getdata(handles.ai);
% compute mean of myarray
toPlot = mean(handles.myarray)
% increment lineselection
handles.counter = handles.counter+1;

% register full, overwrite from first row
if handles.counter==10
   handles.counter = 1;
end


note that you must insert

>> handles = guidata(handles.figure1);

at the beginning of update_plot and

>> guidata(handles.figure1, handles);

at the end of update_plot since handles are sent to
update_plot only at initiation of function as
SamplesAcquiredFcn which you did in openingFcn.





An example of mine looks loke this (slightly shortened):


function update_plot(hObject, eventdata, handles)

handles = guidata(handles.figure1);
     
inpt = getdata(handles.ai,handles.N_in);
  
handles.AvgMat(handles.AvgCount,:) = inpt';
inpt = mean(handles.AvgMatRotate);

if handles.AvgNumCount == handles.rotations
   handles.AvgNumCount = 1;
end

handles.AvgCount = handles.AvgCount + 1;


set(handles.hPlotSpec,'YData', inpt);
       
guidata(handles.figure1, handles);

     
    
  
Hope it helps!
LARS

 

Subject: continuous acquisition

From: Lars

Date: 12 Dec, 2007 20:11:12

Message: 15 of 19


Some correction:
>
> function update_plot(hObject, eventdata, handles)
>
> handles = guidata(handles.figure1);
>
> inpt = getdata(handles.ai,handles.N_in);
>
> handles.AvgMat(handles.AvgCount,:) = inpt';
> inpt = mean(handles.AvgMatRotate);
> handles.AvgCount = handles.AvgCount + 1;

> if handles.AvgCount == 10
> handles.AvgCount = 1;
> end
>
>
>
>
> set(handles.hPlotSpec,'YData', inpt);
>
> guidata(handles.figure1, handles);
>
>
>
>
> Hope it helps!
> LARS
>
>

Subject: continuous acquisition

From: Davide Borelli

Date: 17 Dec, 2007 12:07:15

Message: 16 of 19

Thank you very much, it works greatly!
After two or three minutes it tells me an error:

DataMissed event occurred for the object: nidaq-AI

Is it a problem I should be able to sove with BufferingConfig and daqmem? Or I
need some other skill? I have a 2GHz Pentium pc with only 220Mb of RAM.

Another question: I make my program to save data files every 3*10^6 samples,
that means every minute (I get 50000samples per sec), in order not to have too
huge files (every file is about 47MB). Is there a way to have smaller data files
without stopping and restarting the daq board every minute? I don't loose much
time, just 0,2 sec, but I'd like to make a "more continuous" acquisition!

Anyway, I am vey happy just like that!
Thank you again
Davide B.

Subject: continuous acquisition

From: Lars

Date: 20 Dec, 2007 09:36:39

Message: 17 of 19

Sounds like your hardware 'runs dry' of input-data while
performing some other tasks (i.E. saving a file). In this
case 'getdata' causes an error because nothing is to get. If
you don't really need every single sample find out about the
use of 'peekdata' rather than 'getdata'. A (very
unintelligent) solution could also be to use try/catch.
In my opinion you should find out about the 'LogToDisk' and
'LoggingMode' properties of your hardware to avoid the
discontinuity while performing the daq. Just doubleclick the
AI in workspacebrowser and find out about what can be set in
your special hardware.

If this doesn't help:
There is a specialist around here in this forum. His name is
Rob Purser. Go to his newsreader page and contact him. He
always really helped me a lot.

Subject: continuous acquisition

From: Davide Borelli

Date: 20 Dec, 2007 10:10:30

Message: 18 of 19

The hardware is set to the "Disk&Memory" logging mode.
The great majority f the samples are stored in a file, I
use peekdata to acquire some samples just to plot some
data in order to see what's happening while measuring: I
don't plot every sample.
I noticed that error occurs when I run other application
than Matlab (Internet Explorer, for example), while my
program can run for several minutes if I close every other
application: so, the problem is not too big, I can acquire
my data without listening to music with iTunes! I'd just
like to understand better how my hardware works.
Thank you a lot
Davide B.

Subject: continuous acquisition

From: Lars

Date: 20 Dec, 2007 11:57:26

Message: 19 of 19

Since this is not a MATLAB-problem you actually should try

try
getdata('nidaq',1); % no error, go on
catch
return; % error detected, escape Fcn
end

LARS

Tags for this Thread

Everyone's Tags:

ai daq(2)

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
ai daq Yeelin Chong 18 Feb, 2008 01:30:36
ai daq Lars 5 Dec, 2007 07:45:08
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics