-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlungSegmentsToImages.m
More file actions
200 lines (135 loc) · 8.14 KB
/
Copy pathlungSegmentsToImages.m
File metadata and controls
200 lines (135 loc) · 8.14 KB
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
% Titus John
% May 13, 2019
clc
clear
files = dir(fullfile(pwd, '*.txt'));
fileCount = 1;
cycleCount =1;
wheezeCrackleCount =1 ;
crackleCount = 1;
normalCount = 1;
wheezeCount = 1;
wheezeEventOverallCount = 0 ;
crackleEventOverallCount = 0 ;
AllCrackleTimeStarts = [];
checkErrorTemp = 0;
usableEvents = 0;
%Segemented groundTrutch signals with correponding Fs
Signals = {};
for i = 1:length(files) %this reps the first 200 patients
cycleStart = [];
cycleEnd = [];
cracklePresent = [];
wheezePresent = [];
numCycles = 0 ;
cycleCount = 0;
eventFilename = files(i).name;
temp = strsplit(eventFilename,'.');
recordingLabel = temp{1};
temp2 = strsplit(temp{1},'_');
hardware = temp2{5};
patientNum = eventFilename(1:3);
wavFilename = strcat(temp2{1},'_',temp2{2},'_',temp2{3},'_',temp2{4},'_',temp2{5},'.wav');
if isfile(wavFilename)
%% This is to check if the file exist in the Testing testing split method
fileProcess = 1; %This variable sees if the files exist in Testing random spit
%
% for k = 1:length(TestingFileNames)
% if strcmp(eventFilename,TestingFileNames{k}) %&& files(i).bytes == 0 %making sure the files is really empty
% fileProcess = 1;
% end
% end
%
if fileProcess ==1
[rawWholeSignal,ogFs] = audioread(wavFilename ); %Read the signal in if applicable
allIndexStarts = [];
%% This is for reading the event text files
fileID = fopen(eventFilename);
x = fscanf(fileID,'%f %f %s');
fclose(fileID);
tempEventCount = 0;
wheezeEventCounts = 0;
eventStarts = [];
eventEnds = [];
for eventCount= 1:length(x)
if x(eventCount) == 119.0000 % This represent the ascII for wheeze start
tempEventCount = tempEventCount +1;
wheezeEventCounts = wheezeEventCounts + 1;
wheezeEventOverallCount = wheezeEventOverallCount +1;
AllWheezeTimeStarts(wheezeEventOverallCount) = x(eventCount-1);
eventStarts(tempEventCount ) = x(eventCount-2);
eventEnds(tempEventCount) = x(eventCount-1);
end
end
wheezeEventCounts
%% This is to pick of the normal segments
% if isempty(x)
% dt = 1/ogFs;
% Norig = length(rawWholeSignal);
% rawTime = 0:dt:(Norig*dt)-dt;
%
% normalWindow = .65; % this should be in msec
% eventStarts = 0:.65:rawTime(end);
%
%
% for j = 1:(length(eventStarts)-1)
%
% [d, indexStart] = min(abs( rawTime-round(eventStarts(j),3)));
%
% eventSignal = rawWholeSignal(indexStart:indexStart+(ogFs*.65)); % .65 sec represents the window size
% figure(3)
% plot(eventSignal)
%
% [filter_out] = plotWaveCoeff(eventSignal,ogFs);
% figure(4)
% plot(filter_out)
%
% Fs = 4000;
% if length(filter_out) > 128
% figure(1)
% spectrogram(filter_out,128,120,128,Fs,'yaxis')
%
% imageName = strcat('normal_',patientNum,'_',hardware,'_',num2str(i),'_',num2str(normalCount));
%
%
% set(gca,'XTick',[]) % Remove the ticks in the x axis!
% set(gca,'YTick',[]) % Remove the ticks in the y axis
% set(gca,'Position',[0 0 1 1]) % Make the axes occupy the hole figure
%
% % Save the figure
% cd('CWTNormImages/')
% saveas(gcf,imageName,'png')
% cd ..
%
% normalCount= normalCount +1;
% end
% end
% end
%% This is to pick of the wheeze segments
for j = 1:wheezeEventCounts %numCycles changing to events as opposed to segemented cycles
cycleCount = cycleCount+1;
%time make up
dt = 1/ogFs;
Norig = length(rawWholeSignal);
rawTime = 0:dt:(Norig*dt)-dt;
[d, indexStart] = min(abs( rawTime-round(eventStarts(j),3)));
[d, indexEnd ] = min(abs( rawTime-round(eventEnds(j),3)));
groundTruthSegmentedSignal = rawWholeSignal(indexStart:indexEnd);
%eventSignal = rawWholeSignal(indexStart:indexEnd);
%Going to center the wheeze event in the
%middele of the spectorgram
if (indexStart+(ogFs*.325)) < indexEnd && length(rawWholeSignal) > (indexEnd+(ogFs*.325)) && (indexStart-(ogFs*.325)) > 0
usableEvents = usableEvents + 1;
s = (indexStart-ceil(ogFs*.325));
e = (indexEnd+ceil(ogFs*.325));
eventSignal = rawWholeSignal(s:e); % .65 sec represents the window size
eventSignal = resample(eventSignal,4000,ogFs);
windowEventSignalSplit(eventSignal,ogFs,hardware,wheezeEventOverallCount, patientNum)
end
allIndexStarts(cycleCount) = indexStart;
allIndexEnds(cycleCount) = indexEnd;
normalCount= normalCount +1;
end
end
end
end