-
Notifications
You must be signed in to change notification settings - Fork 1
/
compress_wav.c
207 lines (155 loc) · 5.7 KB
/
compress_wav.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
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
/* compress_wav.c
* 2015 Dec 7 Yinpeng Chen, Acadia University
* MSc student in Computer Science: for eeg data compression
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "operator_wav.h"
#define PI 3.14159265
int main()
{
int i = 0;
int main_choice;
struct sin_coeff sin_coe1;
struct sin_coeff sin_coe2;
struct sin_coeff sin_coe3;
struct sin_coeff sin_coe4;
char * infile_name;
FILE * fp;
char * buffer = malloc(sizeof(char)* 1024 * 2 * 8);
printf("***Welcome to eeg data testing system!!!***\n");
printf("1. read a wav file.\n") ;
printf("2. check the data of loaded wav file.\n");
printf("3. set a sin wav.\n");
printf("4. write the new file\n");
printf("******************************************\n");
scanf("%d", &main_choice);
safe_flush(stdin);
printf("your choice: %d!\n", main_choice);
printf("\n");
if (main_choice == 1) {
infile_name = "4cmotion.wav";
if(( fp = fopen(infile_name, "rb")) == NULL){
fprintf(stderr, "ERROR: cannot open file: %s!\n", infile_name);
return 1;
}
if( fread(buffer, 1, 44, fp) != 44 ||
memcmp(buffer, "RIFF", 4) ||
memcmp(buffer + 8, "WAVEfmt ", 8) ||
memcmp(buffer + 36, "data", 4)
){
fprintf(stderr, "This file is not wav file file! Check the header\n");
fclose(fp);
return 1;
}
// unsigned bps = 16;
//read chunk size
/*
unsigned * pos = (unsigned *) malloc (4);
fseek(fp, 4, SEEK_SET);
fread(pos, 1, 4, fp);
unsigned chunksize = array2data(pos, 4);
printf("chunksize is %u\n",chunksize);
fflush(stdout);
fflush(stdin);
safe_flush(stdout);
safe_flush(stdin);
*/
//read channel number
unsigned * pos =(unsigned *) malloc(4);
fseek(fp, 22, SEEK_SET);
fread(pos, 2, 1, fp);
unsigned channel = array2data(pos,2);
printf("channel number: %u\n", channel);
//read sample rate number
fseek(fp, 24, SEEK_SET);
fread(pos, 4, 1, fp);
unsigned sample_rate = array2data( pos, 4);
printf("sample rate is %u\n", sample_rate);
//read total samples
fseek(fp, 40, SEEK_SET);
fread(pos, 1, 4, fp);
unsigned total_samples = array2data(pos, 4) / 2;
printf("total samples is %u\n", total_samples);
free(pos);
//sleep(1);
//data store section
short * data_store = (short *)malloc(total_samples * 4);
fseek(fp, 44, SEEK_SET);
if ( fread(data_store, 2, total_samples, fp) == 0)
{
printf("I can't read any data from wav file!\n");
}
fclose(fp);
fflush(fp);
/* for( i = 0; i < total_samples; i++)
{
printf("The data_store[%u] is %hd\n", i, data_store[i]);
}*/
unsigned sub_samples = total_samples / 4;
short * channel1_data = (short *)malloc(total_samples);
short * channel2_data = (short *)malloc(total_samples);
short * channel3_data = (short *)malloc(total_samples);
short * channel4_data = (short *)malloc(total_samples);
short * channel_bakup = (short *)malloc(total_samples*2);
for( i = 0; i < total_samples; i++)
{
if(i < sub_samples)
{
channel1_data[i] = data_store[i*4];
channel2_data[i] = data_store[i*4+1];
channel3_data[i] = data_store[i*4+2];
channel4_data[i] = data_store[i*4+3];
}
channel_bakup[i] = data_store[i];
}
//write and generate full(contains 4 channels) wav file
char * outfile_full = "4cfull.wav";
write_wav(outfile_full, sub_samples, channel_bakup, sample_rate, channel);
//find max and min value in subchannels
unsigned channel1_diff = array_diff( channel1_data, sub_samples);
unsigned channel2_diff = array_diff( channel2_data, sub_samples);
unsigned channel3_diff = array_diff( channel3_data, sub_samples);
unsigned channel4_diff = array_diff( channel4_data, sub_samples) ;
printf("The channel1_diff is %u, channel2 %u, channel3 %u, channel4 %u\n", channel1_diff,channel2_diff, channel3_diff, channel4_diff);
//sleep(3);
//generate sine wave
//short * sine_data = (short *) malloc (total_samples);
sin_coe1 = find_best_coeff(channel1_data, channel1_diff, sub_samples, sample_rate);
sin_coe2 = find_best_coeff(channel2_data, channel2_diff, sub_samples, sample_rate);
sin_coe3 = find_best_coeff(channel3_data, channel3_diff, sub_samples, sample_rate);
sin_coe4 = find_best_coeff(channel4_data, channel4_diff, sub_samples, sample_rate);
printf("LOOK at main values:\n");
printf("minA1 is %d, minO1 is %d\n", sin_coe1.A, sin_coe1.omega);
printf("minA2 is %d, minO2 is %d\n", sin_coe2.A, sin_coe2.omega);
printf("minA3 is %d, minO3 is %d\n", sin_coe3.A, sin_coe3.omega);
printf("minA4 is %d, minO4 is %d\n", sin_coe4.A, sin_coe4.omega);
//short * sine_wave = (short *) malloc (total_samples);
short * final_data = (short *) malloc (total_samples);
int mark = 0;
for ( i = 0; i < sub_samples; i++)
{
mark = i;
final_data[mark] = channel1_data[i] - (0.05 * sin_coe1.A * channel1_diff) * sin( 2 * PI * sample_rate * i * sin_coe1.omega);
final_data[mark+1] = channel2_data[i] - (0.05 * sin_coe2.A * channel2_diff) * sin( 2 * PI * sample_rate * i * sin_coe2.omega);
final_data[mark+2] = channel3_data[i] - (0.05 * sin_coe3.A * channel3_diff) * sin( 2 * PI * sample_rate * i * sin_coe3.omega);
final_data[mark+3] = channel4_data[i] - (0.05 * sin_coe4.A * channel4_diff) * sin( 2 * PI * sample_rate * i * sin_coe4.omega);
}
printf("mark is %d",mark);
char * newfile = "4cnew.wav";
write_wav(newfile, sub_samples, final_data, sample_rate, channel);
/*
for(i = 0; i < sub_samples; i++)
{
error_data[i] = channel1_data[i] - sine_data[i];
printf("error data[%u] is %hu\n", i, error_data[i]);
}
*/
//int error_diff = array_diff( error_data, sub_samples);
//printf("error diff is %u, channel1_diff is %u\n", error_diff, channel1_diff);
}
return 1;
}