forked from Digiducer/matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavFileDecoder.m
113 lines (84 loc) · 3.41 KB
/
wavFileDecoder.m
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
function [ CalDate, SN, CalA, CalB, returnVal ] = Wav_file_funct( Wav_file )
%This file reads the format of a wav_file and spits out the calibration
%information
% The program first confirms it's a wave file, finds the data chunk for
% calibration, extracts that data, and sends it to be plotted and
% confirmed
%open the file to read byte by byte
Sensitivity_file = fopen(Wav_file, 'r');
%iterate through and grab the header data
for header_count = 1:4
array = fread(Sensitivity_file, 4, '*char');
if header_count == 1
RIFF_comp = strcat(array(1), array(2), array(3), array(4));
elseif header_count == 2
Arbitrary_size = strcat(array(1), array(2), array(3), array(4));
elseif header_count == 3
WAVE_comp = strcat(array(1), array(2), array(3), array(4));
else
FMT_comp = strcat(array(1), array(2), array(3), array(4));
end
header_count = header_count + 1;
end
%move on only if the correct wav file header was found
if strcmp(RIFF_comp, 'RIFF') == 1 && strcmp(WAVE_comp, 'WAVE') == 1 && strcmp(FMT_comp, 'fmt') == 1
bytes = fread(Sensitivity_file, 1, '*int');
if bytes == 0
errordlg('There is no remaining data to read or seek for. ABORT');
returnVal = -1;
return
end
status = fseek(Sensitivity_file, bytes, 'cof');
if status == -1
errordlg('There is no remaining data to read or seek for. ABORT');
returnVal = -1;
return
end
array = fread(Sensitivity_file, 4, '*char');
array = strcat(array(1), array(2), array(3), array(4));
%continue to look for Cal1 data
while strcmp(array, 'CAL1' ) ~= 1
bytes = fread(Sensitivity_file, 1, '*int');
fseek(Sensitivity_file, bytes, 'cof');
array = fread(Sensitivity_file, 4, '*char');
%if there is no calibration data, set these as defaults and return
%0
if length(array) ~= 4
returnVal = 0;
SN = 'Not Available';
%input nominal values
CalA = 33000;
CalB = 65000;
CalDate = 'Not Available';
return
end
array = strcat(array(1), array(2), array(3), array(4));
end
%%%%% GUYS GUYS! WE FOUND CAL1!!!
bytes = fread(Sensitivity_file, 1, '*int');
Cal1_content = transpose(fread(Sensitivity_file, bytes, '*char'));
Cal1_content = strsplit(Cal1_content, ' ');
if strcmp(Cal1_content(1), '333D01') ~= 1
errordlg('This is not the 333D01 sensor. ABORT!');
end
encodedStr = cell2mat(Cal1_content(3));
%make sure it's the correct version
if str2num(encodedStr(1)) ~= 1
errordlg('This is not formatted to version 1. ABORT!');
end
%place all the data in arrays and return a 1
SN = str2num(encodedStr(2:7));
CalA = str2num(encodedStr(8:12));
CalB = str2num(encodedStr(13:17));
dateString = encodedStr(18:end);
yearTens = dateString(1:2);
month = dateString(3:4);
day = dateString(5:6);
year = ['20',yearTens];
CalDate = [month,'/',day,'/',year];
returnVal = 1;
else
%if not formatted correctly, return -1
errordlg('This is not a properly formatted wav file. ABORT!');
returnVal = -1;
end