Olimex Support Forum

Others => Biofeedback - EEG, ECG, EKG => Topic started by: Lichtamberg on March 29, 2014, 10:43:04 PM

Title: EKG Signal on OSX via Ruby/R for EKG/EMG Shield
Post by: Lichtamberg on March 29, 2014, 10:43:04 PM
Hey,

Im writing a ruby script, which is displaying the data via R for the EKG/EMG Shield (https://www.olimex.com/Products/Duino/Shields/SHIELD-EKG-EMG/open-source-hardware).

You just have to install ruby, the gems which are required at the top and normal R.

I get a signal, but nothing usefull comes out. Since ruby is not so hard to read, I would be happy if you could comment where the problem is?

Thanks:

require 'pry'
require 'serialport'
require 'yaml'
require 'csv'
require "rinruby"

port_str = "/dev/tty.usbserial-A9SFJTX1"  #may be different for you
baud_rate = 57600
data_bits = 8
stop_bits = 1
file = '/Users/peterlampesberger/Desktop/results.yml'
parity = SerialPort::NONE

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
collected_data = []
j = 0

while (i = sp.gets.chomp) do
  break if j > 50000
  arr = i.bytes.to_a
  index = arr.index(165)
  if index
    arr[index..-1].each_slice(17).to_a.each do |found_data|

      data_hash = {
        id: j,
        #sync0: found_data[0],
        #sync1: found_data[1],
        #version: found_data[2],
        count: found_data[3],
        data: found_data[4..-2],
        #switches: found_data[-1]
      }
      if !data_hash[:count].nil? && data_hash[:data].size == 12
        # get the big indian format values
        processed_data_hash = {
          kanal0: data_hash[:data][0..1],
          kanal1: data_hash[:data][2..3],
          kanal2: data_hash[:data][4..5],
          kanal3: data_hash[:data][6..7],
          # kanal4: data_hash[:data][8..9],
          # kanal5: data_hash[:data][10..11]
        }
        # Convert the big indian fomat to binary
        converted_data_hash = {
          kanal0: processed_data_hash[:kanal0][0].to_i.to_s(2) + processed_data_hash[:kanal0][1].to_i.to_s(2),
          kanal1: processed_data_hash[:kanal1][0].to_i.to_s(2) + processed_data_hash[:kanal1][1].to_i.to_s(2),
          kanal2: processed_data_hash[:kanal2][0].to_i.to_s(2) + processed_data_hash[:kanal2][1].to_i.to_s(2),
          kanal3: processed_data_hash[:kanal3][0].to_i.to_s(2) + processed_data_hash[:kanal3][1].to_i.to_s(2),
          # kanal4: processed_data_hash[:kanal4][0].to_i.to_s(2) + processed_data_hash[:kanal4][1].to_i.to_s(2),
          # kanal5: processed_data_hash[:kanal5][0].to_i.to_s(2) + processed_data_hash[:kanal5][1].to_i.to_s(2)
        }
        # Normalize the values
        reformatted_converted_data_hash = {
          kanal0: converted_data_hash[:kanal0].to_i(2).to_f / 1024,
          kanal1: converted_data_hash[:kanal1].to_i(2).to_f / 1024,
          kanal2: converted_data_hash[:kanal2].to_i(2).to_f / 1024,
          kanal3: converted_data_hash[:kanal3].to_i(2).to_f / 1024
          # kanal4: converted_data_hash[:kanal4].to_i(2).to_f / 1024,
          # kanal5: converted_data_hash[:kanal5].to_i(2).to_f / 1024
        }
        collected_data << reformatted_converted_data_hash
        if j % 1200 == 0
          CSV.open("results.csv", "wb") do |csv|
            csv << ["Voltage"]

            collected_data.last(1200).each do |data|
              #puts data
              csv << [(data.values[1] - data.values[0])]
            end
          end
          R.eval "mydata = read.csv('results.csv')"
          R.eval "plot.ts(mydata)"
          sleep 0.1
        end
        j += 1
      end
    end
  end
end
sp.close
Title: Re: EKG Signal on OSX via Ruby/R for EKG/EMG Shield
Post by: Lichtamberg on April 07, 2014, 12:24:27 AM
It would be nice if someone could take a look at it, thanks