-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo_fhr_us.m
155 lines (101 loc) · 5.22 KB
/
demo_fhr_us.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
%%
%OVERVIEW
% This script test if the functions have been properly downloaded and
% set up
clc;clear; close all;
%% Loading function parameters
FHRparams = loadParameters();
%% Estimating FHR from Ultrasound recordings
recordings = dir(strcat(FHRparams.pathRecording,'*wav')); % Loading the wav files contained in the recording folder
totalRecordings = length(recordings); %Total number of recordings
windowLength = 3.75; %Size DusSegment
sliddingWindow = 3.75; %Sliding window
recordingsFHR = struct();
for idxRecordings=1:totalRecordings
%Loading the DUS recording
[dusRecoding,samplingFrequency] = audioread(strcat(FHRparams.pathRecording,recordings(idxRecordings).name));
% Checking the DUS recording has a sampling frequency of 4000 Hz
% In case, the sampling is different, the recording is downsampled
if samplingFrequency~=FHRparams.Fs
dusSignal = resample(dusRecoding,FHRparams.Fs,samplingFrequency);
else
dusSignal = dusRecoding;
end
% The FHR is calculated using a segment of 3.75 seconds every 0.25
% seconds
dusSegmentSize = windowLength*FHRparams.Fs; % DUS window to estimate FHR
shiftWindow = sliddingWindow*FHRparams.Fs; % Slidding window
%Number of windows contained in the DUS recording
numberWindows = floor((length(dusSignal)-dusSegmentSize)/shiftWindow);
numberWindows = numberWindows+1; %Adding last window
%Array to store FHR of each window
fetalHeartRate = nan(numberWindows,1);
%Iterating over DUS recording
endSegment = dusSegmentSize;
startSegment = endSegment-dusSegmentSize+1;
windowIdx=1;
while windowIdx <= numberWindows
% Extracting current segment of 3.75 s
dusSegment = dusSignal( startSegment:endSegment );
% Call FHR estimation function
fetalHeartRate(windowIdx)= getFHR(dusSegment, FHRparams.Fs, ...
FHRparams.minPeriod, FHRparams.maxPeriod, FHRparams.Hd, FHRparams.cutFreq,...
FHRparams.ratioFirstPeak);
% Updating indexes for next segments
endSegment = endSegment+shiftWindow;
startSegment = endSegment-dusSegmentSize+1;
%Store the FHR of the window of the recording
recordingsFHR.(recordings(idxRecordings).name(1:end-4)).(strcat('w_',num2str(windowIdx))).('FHR') = fetalHeartRate(windowIdx);
% Updating window index
windowIdx = windowIdx+1;
end
end
%% Loading files values
fid = fopen('gs_output.csv');
tline = fgetl(fid); %Discard first line scince it is headers.
tline = fgetl(fid); %Second line
while ischar(tline)
atoms = strsplit(tline,','); %Getting each value of the line: 1) recording name 2)window idx 3) FHR
nameRecording = strtrim(atoms{1});%Name of the recording
nameRecording = nameRecording(1:end-4); %Removing file extension
windowIdx = str2double(atoms{2}); %Window of the recording
FHR_gold = str2double(atoms{3});%Gold standard comparison value
FHR_local = recordingsFHR.(nameRecording).(strcat('w_',num2str(windowIdx))).FHR; %Local FHR
%In case the window was able to be estimated, the difference between
%gold standard and local FHR is calcualted
if ~isnan(FHR_gold)
recordingsFHR.(nameRecording).(strcat('w_',num2str(windowIdx))).diff = abs(FHR_gold -FHR_gold); %storing difference
else
%If the gold standard was NaN, the window is not used for comparison
recordingsFHR.(nameRecording).(strcat('w_',num2str(windowIdx))).diff = 0;
end
tline = fgetl(fid); %Next line
end
fclose(fid);
%% Checking results
recordingsNames = fieldnames(recordingsFHR); %Obtaining names of the tested recordings
for iRecordings=1:length(recordingsNames)
windowsRecording = fieldnames(recordingsFHR.(recordingsNames{iRecordings}));
agreeVector = nan(length(windowsRecording),1); %Vector to store the difference between windows
for iWindow=1:length(windowsRecording)
%Storing if the window agreed
agreeVector(iWindow)= recordingsFHR.(recordingsNames{iRecordings}).(windowsRecording{iWindow}).diff;
end
%Suming up window differences
sumDifferences = sum(agreeVector);
% If the difference are zero, output "exact match with gold standar output".
%If the difference is less than 1bpm per minute of data analyzed, output is "Negligible difference with the gold standard (X BPM)"
%If it's larger - "Caution - significant difference with gold standard output - please check data or code are correct".
if sumDifferences==0
score = "Exact match with gold standar output";
elseif sumDifferences<=1
score = strcat("Negligible difference with the gold standard (",num2str(sumDifferences)," BPM)");
else
score = "Caution - significant difference with gold standard output - please check data or code are correct";
end
%Printing results for the recording
fprintf("Name recording: %s\n", recordingsNames{iRecordings} );
fprintf("Total difference along windows: %f bpm\n", sumDifferences);
fprintf("Score comparison: %s Hz\n", score);
fprintf("******************************************\n\n");
end