# Plant Electrical Signal Classification Based on Waveform Similarity
## 3 Materials and Methods
### Our Proposed Method
#### 3.1.3 Template Matching Algorithm to Classify AP  
Common similarity measure methods for time series include Euclidean distance, dynamic time warping (DTW) [82], as shown in Table 1. Considering that the extracellular plant AP recordings are relative measurement which depend on the positions of recording electrodes and the reference electrode, so we employed the cross correlation (CC) function. It is a measure of similarity of two time series as a function of the lag of one relative to the other. The length of two time series must be the same. CC is insensitive to amplitude and time shift. It can be expressed as  
\[r(d)=\frac{\sum\limits_{i}{(x_{i}-\overline{X})(y_{(i-d)}-\overline{Y})}}{ \sqrt{\sum\limits_{i}{(x_{i}-\overline{X})}^{2}}\sqrt{\sum\limits_{i}{(y_{(i- d)}-\overline{Y})}^{2}}} \tag{2.}\]  
Its value is between \(-1\) and \(1\). When the delay time in CC is zero (\(d=0\)), it can be used to get Pearson correlation coefficient (PCC) [83]. Here, we selected Pearson correlation coefficient for two reasons. First, it is simple and fast in calculating. Second, it is not affected by the data amplitude and noise. Other similarity methods based on distance measure will be sensitive to noise data, and cannot distinguish AP and non-AP waveform when their amplitudes are close. We begin with an initial set of N random templates (APs) and the amplitude is transformed to be positive by subtracting the minimum value for every AP, then normalize the APs by Equation (2), and align all peaks based on the peaks' position by extending all AP waveforms, as shown in Figure 2. Next, we calculate the PCC in Equation (2) to classify the type of APs. If PCC between two APs is bigger than 0.9 (\(p<0.01\)), they are similar, then merge them into one AP by calculating the average value of two APs. \[X=\frac{X-\min(X)}{max(X)} \tag{3}\]  
\[r=\frac{\sum\limits_{i=1}^{n}{(X_{i}-\overline{X})(Y_{i}-\overline{Y})}}{ \sqrt{\sum\limits_{i=1}^{n}{(X_{i}-\overline{X})}^{2}}\sqrt{\sum\limits_{i=1}^ {n}{(Y_{i}-\overline{Y})}^{2}}} \tag{4}\]  
where, \(X=[X_{1},X_{2},\ldots X_{n}]\), \(Y=[Y_{1},Y_{2},\ldots Y_{n}]\), \(\overline{X}=\frac{1}{n}\sum\limits_{i=1}^{n}{X_{i}}\), \(\overline{Y}=\frac{1}{n}\sum\limits_{i=1}^{n}{Y_{i}}\). As mentioned above, it allows us to reduce the numbers of templates by merging templates if PCC is more than 0.9 between the two templates. The new template is generated by calculating the average of two similar templates. There are variations in duration and amplitude between the AP templates, e.g., eight AP templates as shown in Figure 3. \begin{table}
\begin{tabular}{p{113.8 pt} p{113.8 pt} p{113.8 pt} p{113.8 pt}} \hline
**Expression** & **Definition** & **Advantages** & **Disadvantages** \\ \hline Euclidean distance & Calculating the distance of two time series in \(N\)-dimensional space & Simple, and supports high-dimensional data & Sensitive to data shift and noise. The length of two time series must be same \\ \hline DTW & DTW is an algorithm for measuring the similarity between two temporal sequences with different length & Estimate the similarity of two time series with unequal length & O(N\({}^{2}\)) for computing complexity \\ \hline \end{tabular}
\end{table} 
Table 1: Common similarity measure methods. AP classification algorithm (Algorithm 2) is described as follows:  
Input: Raw signal represented by ST, AP template represented by AT in database  
Output: AP, non-AP  
1. Normalize ST and AT // Normalize ST and AT by Equation (2)  
2. Align ST with AT // STs are properly aligned with templates  
Append-Threshold = 0.91; //  
Update-threshold = 0.95; //  
3. corr = Pearson correlation coefficient between ST and AT // Computing PCC  
4. IF corr < Append-Threshold  
ST is not an AP signal, reject it. Return FALSE;  
ENDIF  
5. IF corr = Append-Threshold && corr < Update-threshold  
Add ST into APTemp // Add ST into AP Template library  
Return TURE;  
ENDIF  
6.IF corr >Update-threshold  
Merge ST with APTemp;  
Return TURE;  
ENDIF  
Figure 3: A case of eight AP templates.