pluxbiosignals / python-samples

Python programming examples using PLUX's API.
https://pluxbiosignals.com
Apache License 2.0
13 stars 4 forks source link

Acquisition frequency in Python #11

Closed robeees closed 2 years ago

robeees commented 2 years ago

Hello, I am working wirh Biosignal PLUX device. In particular, I am using a respiration PZT (channel 1) and ecg electrodes (channel 2). I started the implementation from MultipleDeviceThreadingExample. Main edits are in OnRawFrame() method.

My idea is to:

Issue: 1) I was expected to obtain data with frequency of 4000 Hz (such as in the opensignal software works and also User manual confirm that with two channels maximum frequency is 5000 Hz), but after run the script below, when I open the .txt file and plot values is not like this ( I obtained a frequency much lower, it seems around 250 Hz). 2) Furthermore, when I register a session of 1 minute, it feels like that only the first part of that registration is stored. When I plot .txt data coming from 1 min of registration, I can see only 3 complex QRS from ECG plot and only 1 wave relative to respiration plot.

This is my script:

` class NewDevice(plux.SignalsDev):

def __init__(self, address):
    plux.MemoryDev.__init__(address)
    plux.BaseDev.__init__(address)
    self.time = 0
    self.frequency = 0
    self.timestamp = time.time()
    self.epoch = []
    self.data_resp = []
    self.data_ecg =[]

def onRawFrame(self, nSeq, data):  # onRawFrame takes three arguments

    if nSeq % 1 == 0:

        print('data: ' + str(data))
        seconds = time.time()
        local_time = time.ctime(seconds)

        print('nSeq: ' + str(nSeq) + " Local time:", local_time)

        n = 16
        resp = ((data[0]/(pow(2,n)-1)-0.5)*3) #formula for convertion of raw data in respiration unit
        ecg = (((data[1]/(pow(2,n))-0.5)*3)/1019)*1000 #formula for convertion of raw data in ECG (mV)

        self.data_resp.append(resp)
        self.data_ecg.append(ecg)

        #save data in .txt file
        savetxt('data_resp.txt', self.data_resp)
        savetxt('data_ecg.txt', self.data_ecg)

    if nSeq > self.time: 
       return True
    return False

`

Can you help me? There is any chance to increase frequency? (I modified nSeq % 2000 to nSeq % 1, that seems the inferior limit)

Thank you in advance

fcachado commented 2 years ago

Hello @robeees,

Answering to your questions:

  1. To acquire signal with high frequency rates, such as 4000 Hz and higher, the biosignalplux device needs to have the firmware version 3.5 or higher User Manual section 7.2.

  2. Taking into account the signals that you are acquiring, ECG and PZT, I would suggest decreasing your sampling rate, because those two signals have a low frequency content, in the ECG datasheet and in the PZT datasheet you can infer the bandwidth specification, so with a sampling rate of 1000 Hz you can acquire both signals without losing any information.

  3. In your code, changing nSeq % 2000 to nSeq % 1 means that the code enters the if clause every time a new sample is received, instead of every 2000 as the sample code.

  4. Looking to your code, in order to maintain your desire sampling rate, I would suggest removing the print() function and also move your save_txt() function to the ending clause, saving both arrays only after the acquisition ends.

  5. Regarding your ending if clause, if nSeq > self.time: you are comparing two different dimensions, to correctly perform the Boolean operation it is necessary to perform a conversion to have both variable at the same dimension. One way is to have both at the time domain (seconds), so the nSeq needs to be divided by the sampling rate to obtain the elapsed time since the start of acquisition and then is possible to access if the elapsed time is higher or not than the defined acquisition time:

    if nSeq/self.frequency > self.time:
      return true