-
Notifications
You must be signed in to change notification settings - Fork 3
/
wave_process.m
249 lines (242 loc) · 7.87 KB
/
wave_process.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
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
clc;
clf('reset');
close all hidden;
%Wave segment will generate new data which has been segmented to aling S1
%and S2 peaks in the audio data
%wave_segment;
%% Load the data from the given directory
filePattern = fullfile('t2','set_a','*.wav');
dirListings = dir(filePattern);
dir_len = length(dirListings);
fileID = fopen('set_a_timing.csv');
data = textscan(fileID,'%s %s %s %s','Delimiter',',');
categories ={'Artifact','Extrahls','Murmur','Normal','Test_Artifact','Test_Extrahls','Test_Murmur','Test_Normal','Unlabelled'};
len = 3099;
%% Here you can modify the number of test files for training
test_artifact = 26;
test_extrahls = 26;
test_murmur = 26;
test_normal = 26;
%% Generate the relevant directory for saving the outputs the files
search_dir = fullfile('t2','images');
rmdir(search_dir,'s');
mkdir(search_dir);
for k = 1:numel(categories)
if(exist(search_dir,'dir') == 7)
mkdir(fullfile('t2','images',char(categories(k))));
end
end
%% The the wave files are read, normalized and filtered and categorized images are produced
disp('Generating Unlabelled data...');
root_input = fullfile('t2','set_a');
root_output = fullfile('t2','images');
%For the unlabelled data
for d = 1:52
[y,fs] = audioread(fullfile(root_input,dirListings(d).name));
%Settings to set position of figure axes
axes('Units', 'normalized', 'Position', [0 0 1 1])
F =linspace(1,600,1000);
%Generate the sectrogram and save gcf to fig
spectrogram(y,500,[],F,fs,'yaxis');
colormap gray;
fig = gcf;
%Configurations to turn off various features of the figure
set(fig,'Visible','off');
colorbar off;
axis off;
iptsetpref('ImshowBorder','tight');
%Save the image to file
frame = getframe(fig);
I = frame.cdata;
I = imresize(I,[525 700]);
imwrite(I,strcat(fullfile(root_output,'Unlabelled',dirListings(d).name(1:end-4)),'.png'),'png');
close all hidden;
clf;
end
% %%
% disp('Generating Artifact data...');
% %For the artifact data
% for d = 53:92
% [y,fs] = audioread(fullfile(root_input,dirListings(d).name));
%
% %The data is normalized using generalized min-max method.
% norm_y = ((y-min(y))/(max(y) - min(y)))*(1+1)-1;
%
% %Settings to set position of figure axes
% axes('Units', 'normalized', 'Position', [0 0 1 1])
% F =linspace(1,600,1000);
%
% %Generate the sectrogram and save gcf to fig
% spectrogram(y,500,[],F,fs,'yaxis');
% colormap gray;
% fig = gcf;
%
% %Configurations to turn off various features of the figure
% set(fig,'Visible','off');
% colorbar off;
% axis off;
% iptsetpref('ImshowBorder','tight');
%
% %Save the image to file
% frame = getframe(fig);
% I = frame.cdata;
% I = imresize(I,[525 700]);
%
% if(d >= 53+test_artifact)
% imwrite(I,strcat(fullfile(root_output,'Test_Artifact',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% else
% imwrite(I,strcat(fullfile(root_output,'Artifact',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% end
% end
% disp('Generating Extrahls data...');
% %For the extrahls data
% for d = 93:119
% [y,fs] = audioread(fullfile(root_input,dirListings(d).name));
%
% %The data is normalized using generalized min-max method.
% norm_y = ((y-min(y))/(max(y) - min(y)))*(1+1)-1;
%
% %Settings to set position of figure axes
% axes('Units', 'normalized', 'Position', [0 0 1 1])
% F =linspace(1,600,1000);
%
% %Generate the sectrogram and save gcf to fig
% spectrogram(y,500,[],F,fs,'yaxis');
% colormap gray;
% fig = gcf;
%
% %Configurations to turn off various features of the figure
% set(fig,'Visible','off');
% colorbar off;
% axis off;
% iptsetpref('ImshowBorder','tight');
%
% %Save the image to file
% frame = getframe(fig);
% I = frame.cdata;
% I = imresize(I,[525 700]);
% if(d >= 93+test_extrahls)
% imwrite(I,strcat(fullfile(root_output,'Test_Extrahls',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% else
% imwrite(I,strcat(fullfile(root_output,'Extrahls',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% end
% end
% disp('Generating Murmur data...');
% %For the Murmur data
% for d = 120:153
% [y,fs] = audioread(fullfile(root_input,dirListings(d).name));
%
% %The data is normalized using generalized min-max method.
% norm_y = ((y-min(y))/(max(y) - min(y)))*(1+1)-1;
%
% %Settings to set position of figure axes
% axes('Units', 'normalized', 'Position', [0 0 1 1])
% F =linspace(1,600,1000);
%
% %Generate the sectrogram and save gcf to fig
% spectrogram(y,500,[],F,fs,'yaxis');
% colormap gray;
% fig = gcf;
%
% %Configurations to turn off various features of the figure
% set(fig,'Visible','off');
% colorbar off;
% axis off;
% iptsetpref('ImshowBorder','tight');
%
% %Save the image to the file
% frame = getframe(fig);
% I = frame.cdata;
% I = imresize(I,[525 700]);
% if(d >= 120+test_murmur)
% imwrite(I,strcat(fullfile(root_output,'Test_Murmur',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% else
% imwrite(I,strcat(fullfile(root_output,'Murmur',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% end
% end
% disp('Generating Normal data...');
% %For the Normal data
% for d = 154:184
% [y,fs] = audioread(fullfile(root_input,dirListings(d).name));
%
% %The data is normalized using generalized min-max method.
% norm_y = ((y-min(y))/(max(y) - min(y)))*(1+1)-1;
%
% %Settings to set position of figure axes
% axes('Units', 'normalized', 'Position', [0 0 1 1])
% F =linspace(1,600,1000);
%
% %Generate the sectrogram and save gcf to fig
% spectrogram(y,500,[],F,fs,'yaxis');
% colormap gray;
% fig = gcf;
%
% %Configurations to turn off various features of the figure
% set(fig,'Visible','off');
% colorbar off;
% axis off;
% iptsetpref('ImshowBorder','tight');
%
% %Save the image to the file
% frame = getframe(fig);
% I = frame.cdata;
% I = imresize(I,[525 700]);
% if(d >= 154+test_normal)
% imwrite(I,strcat(fullfile(root_output,'Test_Normal',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% else
% imwrite(I,strcat(fullfile(root_output,'Normal',dirListings(d).name(1:end-4)),'.png'),'png');
% close all hidden;
% clf;
% end
% end
% %% Loading and processing a bit quicker.
% rootFolder = fullfile('t2','images');
%
% %Train with all data except the unlabelled stuff
% trainData = imageDatastore(fullfile(rootFolder, categories(1:4)), 'LabelSource', 'foldernames');
% testData = imageDatastore(fullfile(rootFolder, categories(5:8)), 'LabelSource', 'foldernames');
% %Show a summary of each labelr
% tb1 = countEachLabel(trainData);
%
% %Determine the minimum no of images
% minSetCount = min(tb1{:,2});
%
% %Split the data into testData and trainData
% %[trainData,testData] = splitEachLabel(trainData,15);
%
%
% %Count the number of test data
% tb2 = countEachLabel(testData)
%
% %Modifying the labels for the trainData
% total = sum(tb2{1:4,2});
% for i = 1:total
% if(testData.Labels(i) == "Test_Artifact")
% testData.Labels(i) = "Artifact";
% elseif(testData.Labels(i) == "Test_Extrahls")
% testData.Labels(i) = "Extrahls";
% elseif(testData.Labels(i) == "Test_Murmur")
% testData.Labels(i) = "Murmur";
% elseif(testData.Labels(i) == "Test_Normal")
% testData.Labels(i) = "Normal";
% end
%
% end
%
%
% %Count the number of training data
% countEachLabel(trainData)