Matlab MEM and ARFilter

Forum for discussion on different signal processing algorithms
Locked
gsudre
Posts: 22
Joined: 05 Mar 2008, 17:03

Matlab MEM and ARFilter

Post by gsudre » 22 May 2008, 11:20

I'm trying to do a sanity check on my data but I haven't been able to succeed and I was wondering if you have any advice. I am trying to compare the output of the ARFilter in BCI2000 to what the mem function returns in Matlab.

The test was simple. I ran SignalGenerator+ARSignalProcessing+CursorTask with the parameters from the example folder and output the ControlSignal from CursorTask every time Feedback==1 (all gains = 1 and offsets = 0). Then, I compare this output to my results running the acquired signal through the MEM function in Matlab, using the same parameters used in BCI2000. No problems here, and both results match perfectly.

The problem arises when I try to use the gUSBAmp as the source. I set the data to float32, change gains and offsets accordingly, and run the same test. In this case, I used EMG for input. Sampling rate was 1200Hz and blocksize=40. When I run through the same steps as above, the overall shape of the outputs looks similar, but there're definitely some scalling/offseting issues.

Do you have any ideas of what might be going on?

Let me know if you need more info about the experiment.

Thanks,

Gus

mellinger
Posts: 1208
Joined: 12 Feb 2003, 11:06

Post by mellinger » 22 May 2008, 12:39

Gus,

what exactly do you mean by "change gains and offsets accordingly"?

Juergen

gsudre
Posts: 22
Joined: 05 Mar 2008, 17:03

Post by gsudre » 22 May 2008, 12:46

I meant that I changed the parameters according to http://www.bci2000.org/wiki/index.php/U ... gUSBampADC
or, in other words, since the type of data saved was already float32 I changed the source gains to 1 and offsets to 0. Like the previous test, those were the same values for the normalizer gains/offsets and linear classifier weights.

Gus

mellinger
Posts: 1208
Joined: 12 Feb 2003, 11:06

Post by mellinger » 22 May 2008, 14:18

Gus,

just to be sure: Adaptation in the Normalizer was switched off, and Normalizer Gain and Offset were set to 1 and 0, respectively?

Slight differences between online and offline computations may occur because in the online system float data are converted to and from double precision multiple times. Finally, in the AppConnector interface, data are transmitted in ASCII format, further reducing numeric precision.

For data that was originally 16 bit, forth-and-back conversions will practically not lose information. For true float data input however, the results computed from the data file will contain slightly more information than the control signal in ASCII representation does. Assuming that the original signal uses a dynamic range of 20 bits of which 4 bits are lost on average due to numeric representation, we would expect a relative error of 2^(4-20) > 1e-5.

So to decide whether your observations are what is to be expected, it would be interesting to know the RMS amplitude of your EMG signal in relation to the amplifier's input range, and how large the observed differences are.

Regards,
Juergen

gsudre
Posts: 22
Joined: 05 Mar 2008, 17:03

Post by gsudre » 23 May 2008, 10:33

Juergen,

Thanks for your answer. Yes, the adaptation was off and those were the values for gain/offset.

I just noticed that AlignChannels in the source module was true, and that is unnecessary for the gUSBamp (according to the wiki). I don't see how this would affect these results, but let me know if you disagree. I'll change it for the next measurements. Another thing I'm unsure of is the use of '-calibrated' when loading this data in Matlab. Supposedly, the data is already in microvolts, correct? So, it wouldn't need to be calibrated. But regardless of using this flag when loading the data, I still cannot make the results match.

To answer your questions, I'm not using the AppConnector interface to output the data. I'm using the technique we discussed in previous posts to save the binary representation of the double in the state vector, and then converting it back in matlab. The RMS of the EMG signal for that run was 176.6894 (min=-1.59e^3, max=1.47e^3, 36360 samples at 1200Hz, 40 samples/block).

Do you think that using int16 would be a better way to save the data so we can match the results? Let me know if you need more info. It's important for us to get at least similar values for the online and offline analysis (like we did in the sine wave experiment) in order to accomplish our goals with future experiments.

Thanks,

Gus

gschalk
Posts: 615
Joined: 28 Jan 2003, 12:37

AlignChannels ...

Post by gschalk » 23 May 2008, 11:36

Gus,

AlignChannels definitely should be off and could account for what you are experiencing. When turned on, it will try to align samples in time (assuming that they were sampled using only one sample-and-hold) using linear interpolation. Thus, this filtering, which occurs AFTER data storage, alters the data and will account for changes.

Also, the results coming from the USBamp are in microvolts, but will be divided by SourceChGain (because, by definition, (data-offset)*gain) is in microVolts. Thus, only if offset is not 0 and gain is not 1 does the calibrated keyword in load_bcidat make any difference.

I definitely would not store data in int16 since they come out of the amp in float. This is for people who know exactly what the range of their data is (so that they can appropriately 'scale' it before storage using SourceChGain) and want to save storage space.

Gerv

gsudre
Posts: 22
Joined: 05 Mar 2008, 17:03

Post by gsudre » 23 May 2008, 12:19

Gerv,

Thanks for the answer. Unfortunately I still see differences between the spectrum amplitudes even after turning aligning off. Indeed, the '-calibrated' flag made no difference for the parameters I'm using (other than getting a vector of singles instead of doubles, which is not a problem).

I kept data storage as float32, but I'm running out of ideas for what can account for the discrepancy. Could the round-off errors that eventually happen in casting be this big?

I can send you pictures by e-mail if necessary.

Thanks,

Gus

gschalk
Posts: 615
Joined: 28 Jan 2003, 12:37

sanity check ...

Post by gschalk » 23 May 2008, 12:27

Gus,

Without knowing more details, the only thing I can think of is that the results match perfectly for the SignalGenerator, but not for the USBamp. However, other than the other Source Module, there is no difference in BCI2000. Because I do not see how it can matter where the data come from, this leads me to believe that there is some other parameterization/filtering that is done in the USBamp case that is not done in the SignalGenerator case.

Why don't you take the SignalGenerator batch/parameter files, and change them to work with the USBamp, and then try again.

Gerv

gsudre
Posts: 22
Joined: 05 Mar 2008, 17:03

Post by gsudre » 23 May 2008, 15:00

Gerv and Juergen,

Sorry about that. I just realized I was taking the difference of channels 1 and 2 in the spatial filter for EMG, but in Matlab I was using just channel 1 to calculate the spectrum. That also explains why the sine wave experiment worked fine.

Anyways, I also tried turning ChannelAlignment on and off, and it does make a difference like you suggested. Now everything matches fine.

Thanks for the patience,

Gus

Locked

Who is online

Users browsing this forum: No registered users and 4 guests