-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrequencyDomainConversion.m
60 lines (53 loc) · 1.56 KB
/
frequencyDomainConversion.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
function [output, filePath, imgOut] = frequencyDomainConversion(data, properties, spectogramParam)
output = [];
%% Variables initialization
Fs = properties.sampling;
windowSize = spectogramParam.windowLength;
overlap = spectogramParam.numOverlap;
%% filter
% data = filter(utilities.highPassFilt, data);
%% extraction
fftLength = spectogramParam.fftLength;
imgOut = stft(data,Fs,'Window',hann(windowSize,"periodic"),...
'OverlapLength',overlap,'FFTLength',fftLength);
imgOut = db(abs(imgOut)); % db
imgOut = imgOut(1:end/2, :); % the second half is redundant
if ~isempty(imgOut)
imgOut = imgOut - min(min(imgOut)); % shift to all positive
imgOut = round((imgOut * (2^16-1)) / max(max(imgOut))); % normalize to 0-(2^16-1)
imgOut = uint16(imgOut);
end
if isfield(spectogramParam,"size") % resizing
if isfield(spectogramParam,"resizeMethod")
method = spectogramParam.resizeMethod;
else
method = 'bicubic';
end
imgOut = imresize(imgOut, spectogramParam.size, method);
end
%% TPF
sampling = properties.sampling;
S = properties.S;
numFlutes = properties.numFlutes;
TPF = (S/60) .* numFlutes;
TPFsample = round(sampling ./ TPF);
%% Label
if isfield(properties, 'Label')
Label = string(properties.Label{1});
end
%% Combine
imgOut(1, end+1) = TPFsample;
imgOut(2, end) = TPF;
imgOut(3, end) = S;
imgOut(4, end) = numFlutes;
%% saving
output = imgOut;
extention = '.jpg';
if 2 <= nargout
filePath = ['.tempSTFT', extention];
imwrite(imgOut, filePath, 'BitDepth', 16, 'Mode', 'lossless');
end
if 3 <= nargout
imgOut = imread(filePath);
end
end