LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamically changing sample rate and samples to generate

Hi, I need help with this one:

I'm planning to generate a sequence of analog waveforms continuously. Every waveform has a different pattern. While they were digitized they were sampled at different sampling rates so in order to reproduce them correctly I have to change the sample rate in the DAQmx timing configuration function. Also, the length of the files (samples) to play is different so the Samples to generate also changes in between the waveforms.

 

I’d like to reconfigure this two parameters (sample rate and # of samples to generate) on the fly (without stopping the task), is this possible?

 

I’m currently using continuous generation with no regeneration. I’m also installing a callback each time SamplestoGenerate reaches certain value to re-write the buffer with new the samples but the problem is that the number of SamplestoGenerate varies on each new regeneration and I don’t know how to change it dynamically while the AO is being generated:

 

 

DAQmxSetWriteAttribute(gTaskHandle, DAQmx_Write_RegenMode, DAQmx_Val_DoNotAllowRegen);

 

DAQmxErrChk (DAQmxCfgSampClkTiming(gTaskHandle,"",samplerate,DAQmx_Val_Rising, DAQmx_Val_ContSamps, SamplestoGenerate));

 

 

I’m using Labwindows CVI/8.1 and a NI USB 6221 with the latest DAQmx driver

 

 

Any help will be appreciated.

0 Kudos
Message 1 of 6
(4,383 Views)
Hey giogonza,

There is no way to reconfigure the sample rate and # of samples parameters without stopping and restarting this task. There may, however, be some workarounds for this.

1) Use an external sample clock and then vary the clock rate. You will then have to configure the external clock to change frequencies after so many samples (the number of samples of your particular waveform).

2) The best method will be to oversample your waveforms. For example, if you have one waveform at 1000Hz with 1000 samples and one waveform at 500Hz with 500 samples then you would resample the 500Hz signal to have 1000 samples and then output at the 1000Hz rate. You are basically outputting the same sample twice in a row. Also, you can probably get an even cleaner signal by interpolating the values in between the known values. Ultimately, This will allow you to achieve the same signal, but at the higher rate. If you resampled all your waveforms to the highest rate then you can output all of them in sequence and even combine them into one waveform.


Message Edited by Chris_D on 06-24-2008 01:21 PM
Regards,

Chris Delvizis
National Instruments
0 Kudos
Message 2 of 6
(4,362 Views)

Hi Chris, as usual thank you for your reply.

 

Your oversampling idea sounds good but unfortunately in my case the waveforms have been already oversampled! so oversampling and oversampled waveform is not a choice to me .  Let me explain you a little more in detail what I’m trying to do:

 

Problem:  I need to continuously generate a sequence of different waveforms. The characteristics of each waveform is quiet different in terms of the sampling rate used to do DAC and the number of samples to generate per waveform. In general terms, I won’t be playing the same waveform twice so I’m using FiniteSamps when configuring the timing of the task. I’m using the internal clock of the device as clock source (I'd really like to use the internal clock of my DAQ board, this makes the hardware simpler).

 

The sampling rates after oversampling are in the range of 1 KHz – 10 KHz (original sample rates are in the range of 250Hz to 500 Hz).

The number of samples can vary between 5000 up to 11000000. (5 sec to 30 min)

 

Example:

Generate Waveform1 (5.7 KHz, 55000samples)àWaveform2 (4KHz , 5000000 samples)àwaveform3 (4096Hz, 11000000samples)à………waveform n. stop

 

As you can see, playing time can vary from min to hours when I play a sequence of waveforms.

 

If possible I don’t want to have silent periods while transitioning from one waveform to another, so the following waveform generation should start as soon as the previous one is done.

 

Since each waveform is different I have a function that reads the sample values and set a big array accordingly: Readrecord ( ). This function works ok.

 

I’m thinking that on every generation of the new waveform I have to reconfigure the sampling rate and the number of samples to generate.

 

If there is no other choice than stopping and starting the task over and over it’s fine as well. The problem is that I don't know how to accomplish this programmatically. Attached is part of the code I've written so far. I'm installing callback called DoneCallback2 in order to stop, clear and then re-start the analog generation once N number of samples has been generated on the previous task. The Readrecord() function reads a file and forms an array called Rawchan1 for the analog output.

For each time the AnalogGenAuto () runs I also generate a digital pulse to indicate the beginning of a new waveform.

My problem is that I can't get the second analog generation to start even though I can see the second digital pulse to be generated correctly once the first generation has reached its end (this tells me than the Donecallback2 function is working properly).  Something is wrong with the logic of my program and I don’t know what.

 

Any help will be appreciated,

 

Thanks,   

0 Kudos
Message 3 of 6
(4,356 Views)
Hey giogonza,

Unfortunately, you will have to stop the task in order to change the sample rate so it looks like you will  have the "silent period". As far as how to implement starting and stopping your task, currently everytime the Donecallback2 function is called you are stopping and clearing your task. Ideally, we only want to create the task once at the beginning of the program and then clear the task once at the very end. There is a time cost to creating and clearing everytime and will cause your "silent time" to be longer. I am thinking this may be part of the reason why your second analog generation is not starting. In between waveforms we only want to stop, reconfigure the sample rate, write the new waveform, and restart the task. Try reconfiguring your code and let me know how it goes.
Regards,

Chris Delvizis
National Instruments
0 Kudos
Message 4 of 6
(4,329 Views)

Hi Chris,

I reconfigured the code following your suggestions and now it looks like the file attached. It seems to work now. The reason why the second analog generation wasn't working is because there was a DAQmxClearTask(taskHandle) command after the error check in the Donecallback2.  I did not catch that error until yesterday. Now my code seems to be doing what I want it to do.

Thank you very much for your help.

 

Cheers,

Giovanni

0 Kudos
Message 5 of 6
(4,309 Views)
Hey giogonza,

I'm glad to hear it works and thanks for posting back your solution!
Regards,

Chris Delvizis
National Instruments
0 Kudos
Message 6 of 6
(4,294 Views)