C-sharp app, signal processing libs

nekrodezynfekatornekrodezynfekator Poznan, Poland
edited January 2016 in Software
Hi, 
I'm trying to write my own application in C#. When my app is ploting samples, wierd samples appear from time to time. Does anyone have any idea what is going wrong? Thanks for your help.


Comments

  • Occasionally the radio will drop a packet. When this happens, it's typically just a single packet at a time. This shouldn't be happening more than once every 30 seconds to a minute though. One way to improve this is by making sure your batteries are full. As your batteries start to lose life, the radio becomes less steady.
  • wjcroftwjcroft Mount Shasta, CA
    Nek, hi.

    Your photo did not come thru. Can you try putting it on another site or say Google Drive? Also if you post (Github) your C# program, other .NET programmers may be helped.

    William

  • nekrodezynfekatornekrodezynfekator Poznan, Poland
    edited January 2015
    I don't know how to edit my last post, so this is my plot:

    https://drive.google.com/file/d/0BwCCA0mNmuFJQzNWTHdXRjdwX1U/view?usp=sharing

    So, it wasn't a problem with the batteries, but I forgot to implement a function to convert 24bit samples into int32.
    Now I'm trying to implement filter function, but i have also some troubles. What is the length of array used for filtering? 5 or longer?
    Here is my github. I don't think it can be useful yet, it is a huge mess.

  • wjcroftwjcroft Mount Shasta, CA
    Nek, hi.

    Thanks for setting up your Github here's a tree view of it,

    https://github.com/nekrodezynfekator/OpenBCI-C-/tree/master/OpenBCI

    The Google Drive image you posted in last entry I THINK is past history now. This was the original image you posted, but no longer relevant now that you have corrected your 24 bit integer conversion. Correct?

    re: filtering. I would not try to implement your own, instead use one of the existing C# libs,

    https://www.google.com/search?q=c#+dsp+library

    Digital filters are passed an ongoing time series of samples, and produce a corresponding output time series.

    William
  • wjcroftwjcroft Mount Shasta, CA
    edited January 2015
    Here's a cool set of signal processing / visualization tools, Symphony by Robert Burke, formerly of MIT Media Lab Europe, then Microsoft,

    http://robburke.net/mle/

    http://robburke.net/projects/symphony/

    http://robburke.net/mle/CodeZone.pdf
    CodeZone article

    HOWEVER, looking around I don't see any online code for this, he has since moved on from MLE, Microsoft. So this could be a dusty relic.

    ---

    Signal Processing

    An important component of Symphony is a real-time signal processing framework that has been used in our applications to process everything from live video to brainwaves. The framework provides a visual interface that allows a designer to drag and drop atomic signal processors that perform operations ranging from wavelet transforms to image differencing. These can be assembled into real-time signal processing networks that are used by an application.

     
  • The orginal image I posted is no longer relevant. I think that I have correct conversion. 

    I'm trying to rewirte filters from OpenBCI Processing software (filterIIR function etc.). Maybe you suggest to do it in another way?
  • wjcroftwjcroft Mount Shasta, CA
    edited January 2015
    > I'm trying to rewrite filters from OpenBCI Processing software
    (filterIIR function etc.). Maybe you suggest to do it in another way?

    If you want to just convert the filters here into C#, that's fine,

    https://github.com/OpenBCI/OpenBCI_Processing/blob/master/OpenBCI_GUI/EEG_Processing.pde
    https://github.com/OpenBCI/OpenBCI_Processing/tree/master/OpenBCI_GUI

    But as you can see from the source, the filter coefficients were derived from another program (I think Matlab). So for example if you wanted to build another type of filter, that involves a fair amount of steps and knowledge of that other app.

    The existing C# DSP libraries likely allow you to specify filters in a more intuitive way, e.g. Butterworth bandpass filter, order 4, from 15 to 20 hz. (A beta band filter).

    https://www.google.com/search?q=math.net+signal+processing
    http://filtering.mathdotnet.com/
    http://filtering.mathdotnet.com/api/

    William
  • I wrote the filters that are in the Processing GUI.  As William suggested, I computed the filter coefficients in Matlab.  You can see the Matlab command that I used in the Processing source code...specifically the file "EEG_Processing.pde":


    One example is the default 1-50 Hz bandpass filter.  As the comment in the source code says, the filter coefficients were computed in Matlab via: 

    //butter(2,[1 50]/(250/2)); %bandpass filter

    Interestingly, when i read the comments in my code for the notch filters (50 and 60 Hz) in Python.  The syntax is nearly identical.

    Note that all of my filters are butterworth filters of relatively low order...I like butterworth filters because of their smooth response in frequency-domain and I like low-order filters because of the minimal artifacts that they introduce in the time-domain.

    Chip
  • It's also important to note that all of the filters in Processing assume that the OpenBCI sample rate is 250 Hz.  If you run at a different sample rate, these hard-coded filter coefficients are all going to be wrong.

    I wish that there was a Processing library (or Java library) for computing butterworth filter coefficients...a library that I trust, that is.  Probably, I should just dig up a textbook and code my own.

    Chip
  • Hi,
    I have next problems. I'm trying to implement filters to my app. I've been fighting with that for a few days. I rewrote filters code from EEG_processing.pde, but unfortunately I received weird output (below). So my next step was to check output from Proccesing software, but it looks fine. I think that I should implement something else, but I don't know what. I used 50hz notch filter and 1-50Hz bandpass filter. Thanks for your help.

  • You are certainly correct that your output doesn't look right.

    As a debugging technique, I would create fake data to send through your filters.  I would create a simple data vector that holds a sine wave at a given frequency, say 10 Hz.  Pass it through your filter.  Does it still look like a sine wave at 10 Hz?  Does it still have the same amplitude?  The amplitude of 10 Hz shouldn't change when passing through a 1-50Hz BP filter nor 50 Hz notch filter.

    Then, I'd repeat this experiment at different frequencies.  As you get close to the edge of the BP filter (or close to the notch) the amplitude should drop, but the shape of the sine wave should always stay nice and sine-shaped.  Does this happen?

    Good luck!

    Chip
  • This is result of my small research:

    (top: before filtering, bottom: after filtering)
    I think that filters are implemented correctly. I used:
    to convert 24bit samples to Int32, so this part is also correct.

    I probably do a mistake in part where I receive data and convert 3bytes hex to decimal.
  • I implemented "interpretBinaryStream" from "OpenBCI_ADS1299.pde" in my app... and I still have the same problem. I don't know where I made a mistake.
  • wjcroftwjcroft Mount Shasta, CA
    edited January 2015
    Nekro, hi.

    I took a quick look at your code in,

    https://github.com/nekrodezynfekator/OpenBCI-C-/blob/master/OpenBCI/Form1.cs [serialPort1_DataReceived() function]

    It does not look to me like this will handle serial receive buffers that are of arbitrary size (less than OpenBCI packet size), or alignment. There are no guarantees that Windows serial input operations align on packet boundaries, or have a given size. As another point, it could reference buffer[-1] if the data received did not begin with an End byte, etc.

    Have you considered a simple state machine approach, see parse_byte_OPENBCI() function ~ line 750 in,

    https://github.com/ChrisVeigl/BrainBay/blob/master/src/ob_eeg.cpp

    William

  • nekrodezynfekatornekrodezynfekator Poznan, Poland
    edited February 2015
    I analyzed the incoming data. Indeed, the problem is with the buffer. My app is losing incoming samples.
  • Now, all is working fine. Thanks for all of your help, tips and hints! 
    (signal correctly recorded from my wirst)
  • wjcroftwjcroft Mount Shasta, CA
    Nekro, thanks.

    Can you sync your mods to your Github so any other C# hackers can benefit? I checked your Form1.cs and it appears to be the previous version.

    Regards,

    William
  • On my Github I uploaded a working ver of my app. I hope it will be useful for someone. All sugestion and comments are welcomed.
  • wjcroftwjcroft Mount Shasta, CA
    Nek, hi.

    A minor comment on your state machine derived from the ob_eeg.cpp (BrainBay OpenBCI driver). That original code kept track of sync loss situations by bumping a counter. And then that counter was made available on the user interface. This is a feature you may want to include in a future revision, as it gives a sense of the 'quality' of the radio link between board and dongle. If the link is noisy or too far away, the counter will increment more often.

    Can you also upload a screen shot of your program in action, so others can see what that looks like?

    Thanks for making this available.

    William
Sign In or Register to comment.