MATLAB Answers

Is there any way of using machine learning for successive image comparison to a template?

43 views (last 30 days)
Cai Chin
Cai Chin on 16 Dec 2020
Commented: Cai Chin on 6 Jan 2021
Hi, I am using MATLAB R2020a on a MacOS. I am currently trying to detect abnormal phase-space trajectories in the 2D space on a cycle-by-cycle basis as part of an ECG signal. Is there any way of training an algorithm with 'normal' trajectories within that signal and then using this ground truth as a template against which to compare the trajectory associated with each new cycle to find and flag abnormal cycles? Within the signal, there would be mostly normal cycles with intermittent periods of abnormality.
Below is an example of what would constitute a 'normal' trajectory:
This is an example of an abnormal cycle, but abnormal trajectories have variable morphologies:
Please note though, that the comparison would not be against a fixed 'normal' template, but rather a normalised template for a particular individual's signal.
Any suggestions would be very much appreciated. Thanks in advance


Matt Gaidica
Matt Gaidica on 17 Dec 2020
Are the abnormalities time-invariant? I.e. if heart rate doubled, but the voltages followed the same trajectories, should it be deemed 'normal'?
Cai Chin
Cai Chin on 17 Dec 2020
Hi, yes, the abnormalities are time-invariant. In each cycle, the co-ordinates are calculated in such a way as to normalise to the cycle length and thus the variation in HR. This means that it is the location of a particular electrical event in the trajectory rather than the absolute timing of it that I would like to compare.

Sign in to comment.

Answers (2)

Aditya Patil
Aditya Patil on 21 Dec 2020
Edited: Aditya Patil on 21 Dec 2020
As per my understanding, you have some ECG data that can be classified as normal or abnormal, and you want to train a machine learning model that classfies it as such.
While it should be possible to train a model to do the classification on the images as shown in the picture, I would recommend using the ECG signals themselves, as you are losing information when converting from data to images.
If the data is then time dependent or sequential, you could use an LSTM model as shown in the Classify ECG Signals Using Long Short-Term Memory Networks example.
If the data instead is not time dependent or sequential, then consider extracting features and training a Neural network model on it.


Show 1 older comment
Cai Chin
Cai Chin on 31 Dec 2020
Hi, thanks for your answer. I used your suggestion based based on an LSTM network to train the network on raw data first but this took about an hour with a 77% classification accuracy. The example suggests using time-frequency analysis for feature extraction to speed up the training process and improve accuracy but this involves altering the method for one-dimensional data:
Is there any way of doing this feature extraction using 2-dimensional input signals. If not this method of feature extraction, is there another method for feature extraction that might be more suitable?
Aditya Patil
Aditya Patil on 4 Jan 2021
Currently, sequence input layer does not support multiple inputs. This is a known issue and it might be fixed in any of the upcoming releases. A workaround is to create an Nx2 array. I cannot comment on appropriate feature extraction technique for ECG, hence I recommend you to ask a separate question regarding preprocessing ECG data, as that would attract more attention.
Cai Chin
Cai Chin on 4 Jan 2021
Hi, I changed the input layer to 2 instead of 1, and it seemed to work fine with a 2xN array.
However, the training is very slow and I would like the accuracy to be higher. The example you suggested which uses an LSTM network suggests using feature extraction with time-frequency analysis of spectrograms. However, it includes the following instruction:
“Because this example uses an LSTM instead of a CNN, it is important to translate the approach so it applies to one-dimensional signals. Time-frequency (TF) moments extract information from the spectrograms. Each moment can be used as a one-dimensional feature to input to the LSTM.”
How would I alter this so I can use the time frequency analysis on 2-dimensional data?

Sign in to comment.

Image Analyst
Image Analyst on 4 Jan 2021
Edited: Image Analyst on 4 Jan 2021
A simple classifier that might work is to take your signal and compute the centroid.
meanX = mean(x);
meanY = mean(y);
Then compute the slope or angle of all the point from the centroid. Then take the histogram of those.
slopes = (y - meanY) ./ (x - meanX);
grid on;
A normal signal would have just 3 angles (3 humps in the histogram) while an abnormal one would have a histogram with all kinds of different angles (lots of humps, or no humps or discernible shape).
Attach your (x,y) data if you need help with this approach.


Cai Chin
Cai Chin on 4 Jan 2021
Hi, thanks for your suggestion. I have now attached the variables representing the x 'values_v' and y 'values_w' co-ordinates. With this approach, would it be possible to then classify the images automatically using the histograms that are generated by applying some sort of threshold approach, rather than having to manually look at the histograms? Could this threshold then be adjusted dynamically based on the trajectory of a particular individual's trace? Thanks
Image Analyst
Image Analyst on 5 Jan 2021
Which mat file is the normal one and which is the abnormal one? Please make it easy for me : Give me code to read in the mat files into x and y variables and plot them. I'll check back tomorrow for it. No time today.
Cai Chin
Cai Chin on 6 Jan 2021
Hi, sorry about that. Please see below:
% Read in mat files
normal_x = load(values_v_105.mat);
normal_y = load(values_w_105.mat);
abnormal_x = load(values_v_109.mat);
abnormal_y = load(values_w_109.mat);
% Plot data
plot(normal_x(1, 1:900), normal_y(1, 1:900));
plot(abnormal_x(1, 1:900), abnormal_y(1, 1:900));

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by