Pacific-Design.com

    
Home Index

1. Caltech

2. Decode LFP h

Caltech / Decode LFP h /


/* 
   California Institute of Technology
   The Andersen Lab, Division of Biology

   File: Decode_LFP.h

   Description: Plexon neural recording system that counts spikes from strobed time 
   value until strobed stop time, then predicts the category and writes it to the 
   serial port.  Note, this package along with the Decode program is the most recent 
   single unit real time system.

    Begin          : June 1, 2001
    Written        : by Kevin Duraj
    Email          : kevin@vis.caltech.edu 
    Collaborator   : Daniella Meeker 
*/

#ifndef __DECODE__
#define __DECODE__
/********************************************************/
/* Includes */
#include "stdafx.h"
#include <fstream.h>
#include <stdio.h>
#include <math.h>
#include "plexon.h"
/********************************************************/
/* Macros */
#define MAX_TIMESTAMPS_PER_READ 200000
#define NUM_CHANNELS 1
#define NUM_DIRECTIONS 8
#define NUM_NEURONS 2
#define PI 3.14159                    // isn't pi defined in math.h or something?  -CG
#define MAX_NUM_UNITS 128
#define SAMPLING_RATE 40000 //in Hz
#define AVERAGE_WINDOW 10.0         // Averaging window for updating a database
#define TIME_CONSTANT 40000.0         // Danniella knows what this is, right?
#define PORT2_DEFAULT 2059
#define TAU 0.2                         //* 50ms time period.
#define MIN_START_CODE 1
#define MAX_START_CODE 8
#define DATA_SUCCESS_CODE 240
#define DECODE_SUCCESS_CODE 239
#define MIN_FAILURE_CODE 241
#define MAX_FAILURE_CODE 245
#define STOP_CODE 254
#define NO_MEMORY_ERROR 1
#define PLEXON_ERROR 2
#define SERIAL_PORT_ERROR 3
#define READ_DATABASE_ERROR 4
#define STOP_BEFORE_START_ERROR 5
#define ALREADY_HAD_START_ERROR 6
#define STOP_WITHOUT_START_ERROR 7
#define WRONG_NUM_NEURONS_ERROR 8
#define INCORRECT_PARAMETER_LIST_ERROR 9
#define SUCCESS_BEFORE_STOP_ERROR 10
#define LAB_VIEW_BAUD_RATE CBR_9600
#define LAB_VIEW_STOP_BITS ONESTOPBIT
#define LAB_VIEW_DTR_CONTROL DTR_CONTROL_DISABLE
#define LAB_VIEW_RTS_CONTROL RTS_CONTROL_DISABLE
#define LAB_VIEW_PARITY NOPARITY
#define LAB_VIEW_BYTE_SIZE 8
char* gFileName = "D:\\Prosthetic\\databaseworkspace\\data.txt";

/**************************************************************************/
/* Sub-Routines */
// Event Determination
bool IsCancelEvent(PL_Event theEvent);
bool IsDataEvent(PL_Event theEvent);
bool IsStartEvent(PL_Event theEvent);
bool IsStopEvent(PL_Event theEvent);
bool IsDecodeSuccessEvent(PL_Event theEvent);
bool IsFailureEvent(PL_Event theEvent);
bool IsDatabaseSuccessEvent(PL_Event theEvent);

// Event Handling
void ProcessStartSignal(PL_Event theEvent, long& startTime, bool& startDecode, bool stopDecode, int& ReachDirection);
void ProcessStopSignal(PL_Event theEvent, long& endTime, bool gotStart, bool& gotStop);
void ReadDataEvent(PL_Event theEvent, int* SpikeSum, int* NumDSPPerChannel);
void CleanupTrial(bool& gotStart, bool& gotStop, double* ReachProb, int* SpikeSum, int NumActiveUnits);

// Command Line Parameters
void Usage(char *progname);
void ReadParameters(int argc, char** argv);

// Setup
bool AllocateMemory(PL_Event **pServerTimeStampBuffer, double** Means[], double** Stds[], int* Spikes[],
                    int NumActiveUnits);
bool SetupPlexon(HANDLE* hServerPollEvent);
bool SetupSerialPort(HANDLE& hComm, LPCTSTR portName);
bool ReadNumNeurons(int& NumActiveUnits);
bool CheckNumNeurons(int NumActiveUnits, int* NumUnitsPerDSP);

// Cleanup
void CleanupPlexon(HANDLE& hServerPollEvent, PL_Event* pServerTimeStampBuffer);
void DeallocateMemory(PL_Event** pServerTimeStampBuffer, double** Means[], double** Stds[]);

// Data In/Out
void WriteOutDecodeData(HANDLE hComm, int index, double Probability, double x, double y);
bool WriteSerialData(HANDLE hComm, char * stringBuffer);
bool ReadInDatabase(double** Means, double** Stds, int NumActiveUnits, int* TrialCounters);
void WriteDatabase(double** Means, double** Stds, int NumActiveNeurons, int* TrialCounters);

// Decoding Calculations
void CalculateProbabilities(int* SpikeSum, int* LFP, double* ReachProb, double** Means, double** Stds,
                            long startTime, long endTime, int NumActiveUnits, int* TrialCounters);
int FindMostProbableDirection(double* ReachProb, double& Probablitiy);
void CalculateXY(int index, double& x, double& y);
int GetNeuronNum(int* NumUnitsPerDSP, int channel, int unit);

// Database Calculations
void CalculateDatabase(int* Spikes, int* LFP, double** Means, double** Stds, int* TrialCounters, long StartTime,  long EndTime,
                       int ReachDirection, int NumActiveNeurons, int NumLFPChannels);

//Select Best Discriminating Directions
void SelectDirections(double** Means, double** Stds, int NumActiveUnits);

// Extra Display Sub-Routines
void DisplayProbabilities(double* ReachProb);
void PrintSpikeSums(int* Spikes, int NumActiveNeurons);
#endif    /*__DECODE__ */