Make a Truth Table
2 views (last 30 days)
Show older comments
To be honest what I want (in general) is to make something like a truth table for my final year project. I will take the data from MATLAB database as an example, since I cannot upload the data to this community due to the file size. The approachment is similar to what I have done in my project
So far what I have done is following (I divide the code in several parts so that all of you can understand what will be produced in each part):
tt = edfread('example.edf');
info = edfinfo("example.edf");
fs = info.NumSamples/seconds(info.DataRecordDuration);
for k=1:width(tt)
for l=2:4
recnum(l) = l;
signum(k) = k;
%t{k,l} = (0:info.NumSamples(signum(k))-1)/fs(signum(k));
y{l,k} = tt.(signum(k)){recnum(l)};
end
end
y = cell2mat(y);
waveletFunction = 'sym8';
for k = 1:width(tt)
[C{k},L{k}] = wavedec(y(:,k),6,waveletFunction);
s{k} = detcoef(C{k},L{k},6);
end
tc_d = linspace(0,20,74);
plot(tc_d,abs(s{1})); grid on; xticks(0:20); title(info.SignalLabels(1))
plot(tc_d,abs(s{2})); grid on; xticks(0:20); title(info.SignalLabels(2))
Then I need to create a truth table to indicate whether the high voltage occurs (in what seconds). For example, please refer to my drawing:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363998/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1364003/image.jpeg)
If you refer to the graph, the voltage that falls above the half will be considered as "high" (and the others considered as "low").
Actually, I can do it manually by looking at the graph and make the tables in Excel, but I hope there is an inbuilt tool in MATLAB to do the same things more efficiently, and in the other hand I can avoid trivial mistake like mistakenly read the graph.
0 Comments
Answers (2)
chicken vector
on 23 Apr 2023
Edited: chicken vector
on 23 Apr 2023
From the graph I see that the peaks marked are, as you say, over the half, but of the limits of the y axis, not of the maximum value. For this reason allow me to do something that I honestly don't like, that is to find the peaks by looking for valeus over 0.6 in the first graph, and over 0.06 in the second.
% Peaks thresholds:
threshold1 = 0.6;
threshold2 = 0.06;
% Determine respective timestamps:
times1 = unique(floor(tc_d(abs(s{1}) >= threshold1)));
times2 = unique(floor(tc_d(abs(s{2}) >= threshold2)));
% Initialise results cell:
truthCell = cell(20,3);
% Loop over time intervals:
for time = 1 : 20
% Build time interval name:
truthCell{time,1} = [num2str(time-1) '-' num2str(time) ' min'];
% Get result of ECG1 for current time interval:
if ismember(time,times1)
truthCell{time,2} = 'True';
else
truthCell{time,2} = 'False';
end
% Get result of ECG2 for current time interval:
if ismember(time,times2)
truthCell{time,3} = 'True';
else
truthCell{time,3} = 'False';
end
end
% Convert cell to table:
truthTable = cell2table(truthCell, ...
'VariableNames', ['Time', 'ECG1', 'ECG2']);
% Optional if you want to write data fo .xlsx:
writetable(truthTable,'TruthTable.xlsx');
2 Comments
chicken vector
on 23 Apr 2023
Not sure i understand what you are asking.
If you want to find the peaks and the corresponding time instants, you can use islocalmax as suggested by @Steven Lord.
% Base data:
threshold = 0.6;
ECG1 = abs(s{1});
% Find idx that are local max and over threshold:
localMaxIdx = islocalmax(ECG1) & tc_d(abs(s{1}) >= threshold));
% Find time instants corresponding to peaks over threshold:
peakTimes = tc_d(localMaxIdx);
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!