06-23-2020 04:37 AM
Hi everybody!
I am working with the DAQmx toolbox and have some data in a while loop. The number of samples I get out of it is not the number of samples I was expecting. I have tried several things and read all the previous questions related to mine (with the links below I got a little bit further but not completely done).
So I have 5 channels as input (normally from a device but now simulates) and I have 2 output channels. With the DAQmx read, my input channels are read. However, not every while loop, they read the same amount of data. I would expect 1 data point from every source once every loop but this is not the case. And because of the multiple samples, I tried taking the mean but still, the number of samples seems off.
I also have tried writing it directly into the .csv file but then I would also not get the expected number of datapoints. I thought that the problem might have been that writing it in a .csv file took to long. That is why I tried it with arrays.
Right now I think my main questions are:
- Why are there different number of samples per while loop?
- How do I make sure that every sample is saved to the .csv file?
Thanks a lot in advance!
https://forums.ni.com/t5/LabVIEW/Create-2D-Array-In-While-Loop/td-p/2293124?profile.language=en
https://forums.ni.com/t5/LabVIEW/How-to-save-data-in-while-loop-to-array/m-p/3966062#M1129686
https://forums.ni.com/t5/LabVIEW/Save-Continuous-Data-while-loop-as-CSV/m-p/2942686#M849775
https://forums.ni.com/t5/LabVIEW/Waveform-chart-data-saving/m-p/3284717#M959829
06-23-2020 05:17 AM - edited 06-23-2020 05:23 AM
Hi Emmy,
@EmmyJCZ wrote:
So I have 5 channels as input (normally from a device but now simulates) and I have 2 output channels. With the DAQmx read, my input channels are read. However, not every while loop, they read the same amount of data. I would expect 1 data point from every source once every loop but this is not the case. And because of the multiple samples, I tried taking the mean but still, the number of samples seems off.
Right now I think my main questions are:
- Why are there different number of samples per while loop?
Please stick in one thread for this same/very related problem!
There is a different number of samples per loop iteration because you don't request a fixed amount of samples. Instead you read "as much as available"! When you need a fixed amount of samples then you should wire a constant at DAQmxRead…
And why don't you cleanup your code:
You don't need to duplicate code when you can use an autoindexing loop to do the same (mean) calculation!
(You can also replace the shift register & BuildArray by a simple autoindexing output tunnel of the loop: same result…)
@EmmyJCZ wrote:I also have tried writing it directly into the .csv file but then I would also not get the expected number of datapoints. I thought that the problem might have been that writing it in a .csv file took to long. That is why I tried it with arrays.
- How do I make sure that every sample is saved to the .csv file?
You are writing all data from your 2D array into the CSV file. Why do you think you write less (or more?) datatpoints than has been acquired?
06-23-2020 06:54 AM
Hi GerdW,
Thanks a lot for your reply. I am sorry that I made a new subject. For me it felt like a new problem but I guess it is still the same problem.
@GerdW wrote:
There is a different number of samples per loop iteration because you don't request a fixed amount of samples. Instead you read "as much as available"! When you need a fixed amount of samples then you should wire a constant at DAQmxRead…
If I wire a constant to my DAQmx Read, will it read the given number per while loop?
I want it to read every 1/1000 seconds and put it in an array with the numbers from every channel in the same row or column (which doesn't matter to me).
Because the output runs different from the loop, I have the feeling that a for loop won't solve my problem but this is the only thing I can think off.
@GerdW wrote:
Why do you think you write less (or more?) datatpoints than has been acquired?
I think I write less samples because of the mean. But when I don't have the mean I get sometimes like 20 samples per loop in one row. When a loop only reads 2 samples, the rest is filled with NaN, making the file unnecessary large and therefore the implementation in Matlab takes a lot more time than necessary.
When I had the write to spreadsheet in the while loop, I think it only wrote the first (or last) read sample to the file, also resulting in less samples then expected. Which also explains why the number of samples was different every time.
Thanks for this question! I understand a bit more of what is happening now. Unfortunately I don't feel closer to the answer but now I get why I get this different number of samples.
I haven't tried to find out if I get the right number of samples when removing the mean and reading all the samples from the file because that is a lot of work and this feels like a really simple problem.
06-23-2020 07:27 AM
Hi Emmy,
@EmmyJCZ wrote:
If I wire a constant to my DAQmx Read, will it read the given number per while loop?
That's the point of requesting to read a fixed number of samples!
@EmmyJCZ wrote:
I want it to read every 1/1000 seconds and put it in an array with the numbers from every channel in the same row or column (which doesn't matter to me).
How many samples to you need to read, when you want to read each ms with a samplerate of 1kS/s?
(Does it make sense to use those Mean function(s) then at all?)
Why do you need to read samples at a loop iteration rate of 1000Hz? This usually will not work reliable, it's recommended to read DAQ samples at 10Hz…
@EmmyJCZ wrote:
I think I write less samples because of the mean. But when I don't have the mean I get sometimes like 20 samples per loop in one row. When a loop only reads 2 samples, the rest is filled with NaN, making the file unnecessary large and therefore the implementation in Matlab takes a lot more time than necessary.
Saving less samples to file after using a mean function might be possible: the mean reduces a dataset of several samples to just one sample…
How do you get "20 samples per loop in a row"? Where are those NaN values are coming from when you read just 2 samples? There is information missing from your side…
@EmmyJCZ wrote:
I haven't tried to find out if I get the right number of samples when removing the mean and reading all the samples from the file because that is a lot of work and this feels like a really simple problem.
When you want to save ALL samples then you should save them ALL to the file: doesn't make sense to apply a Mean function/filter on the samples before saving…
06-23-2020 07:37 AM
Hi GerdW,
I have tried it with a given number of samples. This works really well!
I just put in a mean because then I would get only one sample. Not because I want the mean of anything. So actually it didn't make any sense. I guess I didn't understand what was going on. But now I do!
Right now I multiply the running time with the sample rate and put that in the DAQmx Read. I get that number of samples now!
Thanks a lot for your help! It was really appreciated.