You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
feature extraction on recorded data vs. live data.
1 view (last 30 days)
Show older comments
I found this tutorial on feature extration and training using recorded data. https://www.youtube.com/watch?v=VO0d6EuGpO0
What is the difference between recorded data and live data's feature extraction and training? Thanks!!!!
Accepted Answer
Walter Roberson
on 8 May 2018
Different routines are used to fetch the data that is used for training. Once the data is obtained, the code is the same.
29 Comments
Jun W
on 8 May 2018
Thanks for your time. That's where I don't understand on how to fetch live data.
For example I want to use Arduino's input and fetch the streaming data to SVM every 20ms. I can't manually do that obviously, so what's the appropriate way to do so? I have not found any tutorials that show how to fetch live data yet.
Jun W
on 8 May 2018
what is the step to "save the data automatically and send it to svm"? I feel like it must be something so easy that is considered to be common sense but I don't know it.
Walter Roberson
on 8 May 2018
N = 1000;
data = zeros(1, N);
for K = 1 : N
data(K) = readVoltage(a, 'D4');
end
result = svmclassify(SVMStruct, data);
Walter Roberson
on 9 May 2018
Why are you testing N < inf ? You assigned a finite value to N and you never change N ?
You are expanding the data array for each iteration for as long as the program is running. That is not efficient. It would be more efficient to set a limit on the number of samples to process and to pre-allocate the data array, or else to store in chunks, perhaps using a cell array.
To use this code you would need to have already done an svmtrain() and assigned the result to SVMStruct . It does not make sense to simply pass data to svm -- pass it for what purpose? It only makes sense if you already have a trained SVM classifier and you want to process the incoming data against that classifier.
Walter Roberson
on 9 May 2018
No, you should never fft() individual points. You should collect your N points and fft() that.
You are not using the output of the findpeaks().
You are using a variable p that you have not initialized.
Caution: your y will almost certainly be complex-valued.
What is fftshifts ? There is a routine named fftshift without the s
I am still not sure why you are testing N < inf . Your N cannot be inf unless your Fs = 0.
Walter Roberson
on 10 May 2018
You can put
while true
...
end
around the parts you want to repeat indefinitely.
Your N should be
N = floor(tend*Fs);
I think it is pushing it too far to expect to find 400 peaks in a window of 1920 -- that is not even an average of 5 points per peak.
Walter Roberson
on 12 May 2018
Sorry, you deleted all of your code and I no longer have anything to refer to in order to figure out what v is.
Jun W
on 12 May 2018
Edited: Walter Roberson
on 12 May 2018
wrong image. I noticed if I increase the npeaks to 100, the program would run forever, does it mean that it will keep running until it finds all the peaks?
Im also trying to save data into one file. But if I limit N then find y peaks, the result would be different than 'find x peaks within each frame then save all the peaks in N samples in one file'. How can I achieve the latter?
Fs=96000;
tend=20e-3;
N = floor(tend*Fs);
v=zeros(N,1);
t=milliseconds(v);
t0=datetime('now');
for ii=1:N
v(ii)=readVoltage(a,'A0');
t(ii)=datetime('now')-t0;
end
%find 5 peaks
[pks,loc]=findpeaks(real(fft(v)),'npeaks',5);
MYstruct=[pks,db(pks),loc];
Walter Roberson
on 12 May 2018
Unfortunately it is not possible to format a duration object in terms of milliseconds only, or even in terms of seconds and milliseconds. The closest you can get is 'mm:ss.SSS'
Walter Roberson
on 12 May 2018
You are not reading the time, you are recording the time. If you want the time in your duration object t expressed in milliseconds then use
seconds(t) * 1000
You need to decide whether you are only interested in an average sampling rate or if the precise timestamps are important.
If you are only interested in the average sampling rate then you should be taking t(end)/N to get the average -- for example it might tell you that you received samples on average every 0.0005391 seconds, which would be about 1855 samples per second. This kind of calculation would be meaningful with the fft() you are doing.
If you are interested in knowing exactly when each sample was recorded, then you cannot use fft() the way you are, as fft() assumes consistent sampling rate. To use the timing of each individual sample, you need to look in the File Exchange to at the NFFT (Non-Uniform FFT) contribution.
Jun W
on 12 May 2018
Edited: Walter Roberson
on 12 May 2018
- I already defined the sampling rate to be a CONSISTENT fs=96000hz.
- I defined tend=0.02s.
- I defined during timeframe 0.02s, it should get 1920 points, then find 5 peaks from those 1920 points.
- The result for the loc table should be 0.003,0.007,0.010,0.014(because the max is tend=0.02)
- Then I checked the time object to show me the timestamp of each sample it finds, and it takes 0.069s to get one sample, which is different than my definition: 0.02/1920=0.00001042s/sample.
Walter Roberson
on 12 May 2018
You defined a variable to contain 96000. You calculate number of samples, N based upon that variable. You read N samples as quickly as practical. At no point do you tell any hardware that the sampling rate is 96000.
With the data collection method you are using, the only control you have is over the serial port speed in talking to the arduino -- and if you are using a USB connection then the serial port speed is pretty much ignored.
It takes 0.069 seconds to get the first sample. The times between the samples are somewhat variable, but if you mean(diff(t)) then you will see that the average is about 0.0221 which is about 45 Hz.
Jun W
on 12 May 2018
How come I can define sampling rate in audioRead(d,fs) of the PC's soundcard but not a hardware?
Walter Roberson
on 12 May 2018
When you use audioRead() you are taking to directly connected device, either built directly into your computer or a card installed in the computer; through the direct connection it can be told what sampling rate to use. Sound cards are designed to read multiple samples without CPU intervention, capturing batches of signals and making them available to the system. The more samples at a time that you read with a sound card, the less intervention is needed; the fewer samples at a time that you read, the more interruptions have to happen. The interruptions to handle groups of samples are the expensive part.
Your configuration is doing the equivalent of asking for one sample at a time, and only one. It has to send out the instructions to a remote system, wait for the data to be collected, then wait for the data to be transferred back, and wait for your operating system to finish running the virus checking on the spam email that just arrived. Your configuration does not permit you to ask for (say) 1420 samples before the data is returned. You would need to use a different configuration for that.
Walter Roberson
on 12 May 2018
What model of piezo sensor? I am trying to research the data rates and accuracy it has, and trying to research what kind of hardware card you would need to be able to read data at the rate you wanted.
Walter Roberson
on 12 May 2018
That appears to be a fairly decent module. I see that it outputs voltage of up to 15 volts DC; I gather it does so continuously, not going through any kind of discrete conversion.
If you use the built-in analog voltage sensors on the Arduino, then those emit 10 bit readings (0 to 1023), but it might be the case that the values are being scaled before being sent to MATLAB -- I do not know. I would need to see a range of voltage readings to say.
"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."
That is the best case situation, with code dedicated to reading the sensor. It is close to 10 times slower than you want. The number does not take into account the time required to convert or transmit samples to the PC.
I would suggest that if you want 96000 samples per second that you reconsider using Arduino. For example you might use National Instruments PCI-6010 (only available until September), or PCIe-6320, or USB-6003; any of those could be used with the Instrument Control Toolbox. (I would be more comfortable with the PCI or PCIe devices.)
Walter Roberson
on 13 May 2018
The NI PCI device has a dedicated micro-controller that can run a programmable number of data collection cycles at programmed rates. With the PCI or PCIe version it will use DMA (Direct Memory Access) to make the data available to the drivers, which can request that MATLAB be interrupted to run a callback. The PCI device I mentioned is about 10 times faster than the best you could do with the built-in arduino analog convertor, and the PCIe device I mentioned is about 25 times faster than the best you could do with the built-in arduino analog convertor.
Jun W
on 13 May 2018
Both Raspberry pi zero and Teensy 3.2 has a dedicated microcontroller/DMA access. when you say it is 10/25 time faster, do you mean the adc speed?
Walter Roberson
on 13 May 2018
Yes the adc on the arduino is about 10000 samples per second. The national instruments devices I listed are rated as 100000 samples per second or 250000 samples per second.
Jun W
on 13 May 2018
NI device is also external device, which means it also falls into the following category
1. "for each sample, it takes certain amount of time to get to the device and return",
2. or "write some code to run on the Device which waits for a signal from the PC host, and when it gets it, goes ahead and records a number of samples from the analog device as quickly as it can, and when the batch of data is ready, it sends the entire batch back to the PC host. The overhead can be much lower for this kind of setup, but the trade-off is that the data is not continuously available."
Walter Roberson
on 13 May 2018
The NI PCI and PCIe devices are cards, so they have direct communication with the host including DMA. The NI USB device is external.
Jun W
on 13 May 2018
Edited: per isakson
on 13 May 2018
cards. can i use this card instead? https://www.amazon.com/gp/product/B01N905VOY/ref=oh_aui_detailpage_o07_s02?ie=UTF8&psc=1
Walter Roberson
on 13 May 2018
That device is for a consumer level mono microphone input, which would be expecting high impedance and about 0.86 volts AC peak-to-peak. Your device is 15 V DC. I do not think it can produce negative voltages, but I am not certain; I do not know enough about how those devices work and I cannot find the specification sheet for that particular device.
At the very least you would have to put it through a resistor to lower the voltage, and I would be concerned about the current capacity.
I do not know. That adapter looks very cheaply made, and I would not trust it for this purpose.
More Answers (0)
See Also
Categories
Find more on National Instruments Frame Grabbers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)