-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompress_final.cpp
298 lines (265 loc) · 11.8 KB
/
compress_final.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// compress_final.cpp
// Created by Hanna Schamis & Jessica Micallef
//
// Last edit by Jessica Micallef on 11/21/2014
//
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#define ENERGY_WORDS 1024
#define COE_WORDS 6
#define ADC_HEADERS 8
#define MINRANGE 32
#define ALL_HEADERS 40
#define ALL_WORDS 1070
#define WORDS_WITHOUT_MINRANGE 1038
using namespace std;
////////////////// Flip Endianness /////////////
void FlipStuff(uint16_t *WordArray){
uint16_t Half1, Half2;
for (int h=0; h<ENERGY_WORDS+ADC_HEADERS+COE_WORDS; h++){
Half1=(WordArray[h]<<8);
Half2=(WordArray[h]>>8);
WordArray[h]=((Half1&0xFF00)|(Half2&0x00FF));
}
}
/////////////// Flip Endianness Back ////////
void FlipBack(uint16_t *WordArray){
uint16_t Half1, Half2;
for (int k=0; k<ALL_WORDS; k++){
Half1=(WordArray[k]<<8);
Half2=(WordArray[k]>>8);
WordArray[k]=((Half1&0xFF00)|(Half2&0x00FF));
}
}
//////////////// Function that finds the minimum and the ranges of uncompressed file///////////
void FindMinRange(uint16_t *WordArray){
uint16_t MINIMUMS[16] = {0};
uint16_t MAXIMUMS[16]= {0};
uint16_t RANGE[16] = {0};
//Take the first 16 energy words of the file and set those as the minimums and maximums
for (int i = 8; i < 24; i++){
MINIMUMS[i-8]=(WordArray[i]&0x3FFF);
MAXIMUMS[i-8]=(WordArray[i]&0x3FFF);
}
//Check each of the 64 samples against its own channel (%16 assigns each check to be made on the coresponding channel)
uint16_t Temporary;
for (int i = 24; i < ENERGY_WORDS+ADC_HEADERS; i++){
Temporary = (WordArray[i]&0x3FFF);
if (Temporary>MAXIMUMS[(i-8)%16]){
MAXIMUMS[(i-8)%16]=(WordArray[i]&0x3FFF);
}
if(Temporary<MINIMUMS[(i-8)%16]){
MINIMUMS[(i-8)%16]=(WordArray[i]&0x3FFF);
}
}
//Fill all energy words into temporary array
uint16_t WordArrayEnergyTemp[ENERGY_WORDS+COE_WORDS] = {0};
for (int b = 0; b < ENERGY_WORDS+COE_WORDS; b++){
WordArrayEnergyTemp[b] = WordArray[b+8];
}
//Move energy words to start at spot 40 to make room for minimums and ranges:
for (int j=40; j< (40+ENERGY_WORDS+COE_WORDS); j++){
WordArray[j]=WordArrayEnergyTemp[j-40];
}
//Put in minimums after the 8 headers
for (int k=8; k<ADC_HEADERS+16; k++){
WordArray[k]=MINIMUMS[(k-8)%16];
}
// Calculate differences;
for (int j=0; j<16; j++){
RANGE[j]=MAXIMUMS[j]-MINIMUMS[j];
}
for(int i = 0; i < 16; i++){
for (int j=1; j<=7; j++){
if (j==7){
WordArray[i+24]=14; //Range
WordArray[i+8]=0; //Minimum
cout << "Ranges more than 7! At packet " << (WordArray[1]&0x000F) << " event " << (WordArray[1]&0x00F0) << " - channel " << i << endl;
}
if (RANGE[i]<pow(2,j)){ /* Changed to >= so if you have 4, 8, 16, etc, bits it'll round up. */
WordArray[i+24]=j;
break;
}
}
}
}
////////////// Function that compresses a single packet ///////////
int Compress(uint16_t *decompWords, uint16_t *CompWords){
// Center of Energy
uint16_t CenterOfEnergy[6]={0};
for (int i = 0; i<COE_WORDS; i++){
CenterOfEnergy[i]=decompWords[ALL_HEADERS+ENERGY_WORDS+i];
}
// Range
uint16_t Range[16]={0};
for (int i=24; i<40; i++){
Range[i-24]=decompWords[i];
}
// declare the new array (where compressed data will go)
for (int i = 0; i<ALL_HEADERS;i++){
CompWords[i]=(decompWords[i]|0xC000);
}
//calculate differences
uint16_t Diffs[ENERGY_WORDS] = {0};
for (int i=0; i<ENERGY_WORDS; i++){
Diffs[i]=(decompWords[i+40]&0x3FFF)-decompWords[(i%16)+8];
}
//calculate number of rows that 16 channels will compress into (sum of the ranges, round up to integer)
int Rows=0;
int TotalBits = 0;
for (int i = 0; i < 16; i ++){
TotalBits = TotalBits + Range[i];
}
Rows = TotalBits/14;
if(TotalBits%14 != 0){
Rows=Rows+1;
}
//Create binary compressed words
int Counter = 0; //Size of range in bits as running sum for the row
uint16_t Binary[1024] = {0}; //use 1024 because that's the maximum rows (binary words) that it could have.
int BinPlace = 0; //indicates which row it's doing binary for.
uint16_t Temp = 0;
int DecompPlace;
for (int i = 0; i<ENERGY_WORDS; i++){
if ((i%16==0)&&(i!=0)){
Counter = 0;
BinPlace++;
}
Temp=Diffs[i]<<Counter;
DecompPlace = (i%16)+24;
if (Counter+Range[i%16] <=14){
Binary[BinPlace]=((Binary[BinPlace]|Temp)&0x3FFF);
Counter += Range[i%16];
if (Counter == 14){
Counter=0; // reset counter
if(BinPlace!=Rows){
BinPlace++; // next "row"
}
}
}
else {
Binary[BinPlace]=((Binary[BinPlace]|Temp)&0x3FFF);
Binary[BinPlace+1]=((Diffs[i]>>(14-Counter))&0x3FFF);
BinPlace++;
Counter = Range[i%16] - (14-Counter);
}
}
BinPlace = BinPlace+1; //Round up the last row to make sure last compressed word included
//Use the number of rows to keep only the compressed data
for (int k = 0; k < BinPlace; k++){
CompWords[40+k]=(Binary[k]|0x8000);
}
//Add COE words to the end of the compressed array
int CompVar;
for (int i = 0; i < COE_WORDS; i++){
CompVar = i+40+BinPlace;
CompWords[CompVar] = CenterOfEnergy[i];
}
//Changing headers 3 and 4
uint16_t NWords=BinPlace+6+COE_WORDS; // Total number of compressed words + COE words
CompWords[2]=((CompWords[2]&0xFF00)|(NWords>>8));
CompWords[3]=((CompWords[3]&0xFF00)|(NWords&0x00FF));
return BinPlace; //Return BinPlace to use to find total number of compressed words in array
}
int main(){
uint16_t DecompressedWords[ALL_WORDS] = {0}; //Array to store single packet of decompressed words, read in from file
uint16_t CompressedWords[ALL_WORDS] = {0}; //Array to store final compressed words
uint16_t CenterOfEnergy[6] = {0}; //Array to store COE information from the file
int TotalCompWords; //Total energy words after compression + 8 headers + 16 min + 16 ranges + 6 COE words
int BinPlace = 0; //Total energy words after compression
int RunningCount = 0; //Sum of the total words written to file
int LastEvent = 0; //Stores the last header's event stamp
int TotalInput; //Total number of bytes in input file
int PacketMax; //Total number of packets in input file
int CheckPacket = 0; //Integer than goes from 0 to 15 that should match the packet stamp on incoming packets
int CheckPacket2 = 0; //Integer than goes from 0 to 15 that should match the packet stamp on outgoing packets
//Set parameters to read and write files
char read_file[256];
char write_file[256];
FILE * run_file;
FILE * output_file;
//Specify the nodes and slots to run through (integers in file names)
for (int node=5; node<6; node++){
if((node==103)|(node==106)|(node==111)|(node==113)){continue;}
for (int slot=7; slot<8;slot++){
if(slot==12){continue;}
CheckPacket=0;
CheckPacket2=0;
//Open files to read from and write to
sprintf(read_file, "/mydaq/crymph/uncompressed/run15703_node%d_slot%d_uncompressed.bin", node, slot);
sprintf(write_file,"/sudaq/jessimic/run15703_node%d_slot%d_compressed.bin", node, slot );
run_file=fopen64(read_file,"rb");
output_file = fopen(write_file,"wb");
//Find how many words in file
fseek(run_file, -2, SEEK_END);
TotalInput = ftell(run_file)/2; //Gives size of file, divided by 2 bytes to find how many words;
PacketMax = (TotalInput/WORDS_WITHOUT_MINRANGE); //Divde by the number of words in uncompressed file to find total number of packets
rewind(run_file); //set position inside file to beginning
cout << "Working on Node " << node << " Slot " << slot <<"."<<endl;
//Take each Packet individually to compress
for (int H=0; H<PacketMax; H++){
if (run_file == NULL){cout << "Run File Null at " << H << endl;}
//Read words into Decompressed Array
fread(DecompressedWords, sizeof(uint16_t),ADC_HEADERS+ENERGY_WORDS+COE_WORDS, run_file);
//Flip the endianess of the energy words;
FlipStuff(DecompressedWords);
if((DecompressedWords[1]&0x00F0)==LastEvent && (DecompressedWords[1]&0x000F)==0){
//cout << "SAME EVENT AFTER 16 PACKETS AT " << H << endl;
}
//Checks done for Decompressed Array
//cout << "Decomp Event Number = " << (DecompressedWords[1]&0x00F0) << " packet = " << (DecompressedWords[1]&0x000F) << endl;
if((DecompressedWords[1]&0x000F) != CheckPacket){ //compares packet stamp in header to check packet to see if packet skipped
cout << "SKIPPED A PACKET IN DECOMP PACKET " << H << " Event Number = " << (DecompressedWords[1]&0x00F0) << " packet = " << (DecompressedWords[1]&0x000F) << endl;
}
if (CheckPacket < 15){
CheckPacket++; //Increase CheckPacket number by one, if packet dropped this will not match
}
else{
CheckPacket = 0; //reset CheckPacket once at 15 since the next packet should have stamp 0
}
if((DecompressedWords[1]&0x000F) == 15){
LastEvent = (DecompressedWords[1]&0x00F0);
}
if((DecompressedWords[1]&0x00F0)==LastEvent && (DecompressedWords[1]&0x000F)==0){ //Check Event is new after 16 packets
//cout << "SAME EVENT AFTER 16 PACKETS AT " << H << endl;
}
//Insert COE words into the last 6 places of the Decompressed Words Array
for (int i = 0; i < COE_WORDS; i++){
CenterOfEnergy[i]=DecompressedWords[ENERGY_WORDS+ADC_HEADERS+i];
}
//Find the Min & Max in uncompressed file, use to create Min & Range headers
FindMinRange(DecompressedWords);
//Compression Function
BinPlace = Compress(DecompressedWords, CompressedWords); //Call compress function, return number of compressed energy words
TotalCompWords = BinPlace + ALL_HEADERS + COE_WORDS; //find total words in compress packet including headers, min, range, energy words, and COE words
//Checks done on Compressed Word Array
//cout << "Comp Event Number = " << (CompressedWords[1]&0x00F0) << " packet = " << (CompressedWords[1]&0x000F) << endl;
if((CompressedWords[1]&0x000F) != CheckPacket2){ //compares packet stamp in header to running integer
cout << "SKIPPED A PACKET IN COMP PACKET " << H << " Event Number = " << (CompressedWords[1]&0x00F0) << " packet = " << (CompressedWords[1]&0x000F) << endl;
}
if (CheckPacket2 < 15){
CheckPacket2++; //Increase CheckPacket number by one, if packet dropped this will not match
}
else{
CheckPacket2 = 0; //reset CheckPacket once at 15 since the next packet should have stamp 0
}
//Change Endianness again for output
FlipBack(CompressedWords);
//Write Compressed packet to output file
fwrite(CompressedWords, sizeof(uint16_t), TotalCompWords, output_file);
RunningCount = RunningCount + TotalCompWords; //Sum of total words written to file, used for checks
BinPlace = 0; //reset the compressed word count
}
cout << "Node #" << node <<" , Slot #" << slot << " done!" << endl;
fclose(run_file);
fclose(output_file);
}
}
return 0;
}