-
Notifications
You must be signed in to change notification settings - Fork 0
/
nptLoadingEngine.c
169 lines (148 loc) · 4.38 KB
/
nptLoadingEngine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//nptLoadingEngine.c
#include "nptLoadingEngine.h"
/*int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("Usage: nptLoadingEngine <filename>\n");
exit(0);
}
nptHeader header;
header.headersize = 0;
header.num_spikes = 0;
header.channels = 0;
header.timepts = 32;
getSpikeInfo(argv[1], &header);
short int *data = malloc(header.num_spikes*header.channels*header.timepts*sizeof(short int));
int index[100];
fillArray(index, 0, 100,1);
printf("t\n");
getWaves(argv[1], &header, index, data);
int i;
int start = header.channels*header.timepts;
for(i = start; i < start + header.channels*header.timepts; i++)
{
printf("%d ", data[i]);
}
free(data);
return 0;
}*/
nptHeader* getSpikeInfo(const char *fname, nptHeader *header)
{
FILE *f;
int hs,ns;
char chs;
f = fopen(fname, "r");
if(f==NULL)
{
return header;
}
hs = 0;
ns = 0;
chs = 0;
fread(&hs, sizeof(int), 1, f);
hs = CFSwapInt32LittleToHost(hs);
header->headersize = hs;
fread(&ns, sizeof(int), 1, f);
ns = CFSwapInt32LittleToHost(ns);
header->num_spikes = ns;
fread(&chs, sizeof(char), 1, f);
//chs = CFSwapInt16LittleToHost(chs);
header->channels = chs;
fclose(f);
header->timepts = 32;
return header;
}
short int* getWaves(const char *fname, nptHeader *header, unsigned int *index, unsigned int index_length, short int *data)
{
FILE *f;
int chs,pts,waveLength,i,j,status;
short int buffer;
f = fopen(fname, "r");
if(f==NULL)
return NULL;
chs = header->channels;
pts = header->timepts;
waveLength = chs*pts;
for(i = 0; i < index_length; i++ )
//for(i = 0; i < header->num_spikes; i++)
{
status = fseeko(f, header->headersize+index[i]*2*waveLength, SEEK_SET);
if(status!=0)
{
fprintf(stderr,"fseek could not complete");
}
buffer = 0;
for(j = 0; j < waveLength; j++)
{
fread(&buffer, 2, 1, f);
buffer = (short int)CFSwapInt16LittleToHost((unsigned short int)buffer);
//if( (i==111) & (j>=17*32) & (j<18*32) )
// fprintf(stderr,"%d : %d : %d \n",index[i],ftello(f),buffer);
data[i*waveLength + j] = buffer;
}
}
fclose(f);
return data;
}
short int* getWavesForChannels(const char *fname, nptHeader *header, unsigned int *index, unsigned int index_length, unsigned int *channels, unsigned int nchannels,short int *data)
{
FILE *f;
int chs,pts,waveLength,i,j,status,offset,readSize;
short int buffer;
f = fopen(fname, "r");
if(f==NULL)
return NULL;
chs = header->channels;
pts = header->timepts;
waveLength = chs*pts;
//offset to where we should start reading
offset = channels[0]*pts;
readSize = (channels[nchannels-1] - channels[0])*pts;
for(i = 0; i < index_length; i++ )
//for(i = 0; i < header->num_spikes; i++)
{
status = fseeko(f, header->headersize+index[i]*2*waveLength + offset*2, SEEK_SET);
if(status!=0)
{
fprintf(stderr,"fseek could not complete");
}
buffer = 0;
for(j = 0; j < readSize; j++)
{
fread(&buffer, 2, 1, f);
buffer = (short int)CFSwapInt16LittleToHost((unsigned short int)buffer);
//if( (i==111) & (j>=17*32) & (j<18*32) )
// fprintf(stderr,"%d : %d : %d \n",index[i],ftello(f),buffer);
data[i*readSize + j] = buffer;
}
}
fclose(f);
return data;
}
unsigned long long int* getTimes(const char *fname, nptHeader *header, unsigned int *index, unsigned int index_length, unsigned long long int *data)
{
FILE *f;
int i;
unsigned long long int buffer;
f = fopen(fname, "r");
for(i = 0; i < index_length; i++ )
{
fseek(f, header->headersize+2*header->num_spikes*header->channels*header->timepts + index[i]*8, SEEK_SET);
buffer = 0;
fread(&buffer, sizeof(buffer),1,f);
buffer = CFSwapInt64LittleToHost(buffer);
data[i] = buffer;
}
fclose(f);
return data;
}
/*void fillArray(int *array, int start, int end, int step)
{
int i;
int mstep = floor((end-start)/step);
for(i = 0; i < mstep; i = i++)
{
array[i] = start + i*step;
}
}*/