Prerequisites
- In order to make the code work correctly we need to have the library PeakUtils install. You can find this at https://pypi.python.org/pypi/PeakUtils/1.0.0
- Additionally we need matplotlib, numpy, scipy
- "SS_phonocardiogram_peak_detection.py" contains the main code which shows an example using a heart sound signal or PCG store in a .wav file
- "mdatabase.m" contains the PCG data vector and its information (fs, etc.)
- "Filters1.mat" contains the coeficients of a filter
The first derivative gives the slope of a signal at each point (That’s a fact!). In a sound file (heart sound signal for example) when we calculate the first derivate of the vector the set of values of the growing part of the signal results in a positive slope and the set of values of the decreasing part results in negative slope. As a peak happens when the trend change from upward to downward (top of the fig. 1st vector, position 136), we can also think that a peak is going to happen when the values change from positive to negative (top of the fig. 2nd vector, position 136) as its showed below :
Figure 1 also show at the top that each peak is represented as the first negative value of the derivative vector. Therefore, to get the set of all peaks we can just take the positives values or the greater than zero from the derivative vector ‘Derivative’. As a result we will get a logic vector ‘Positives’ with zeros when the value is low than zero and ones when is greater. We will need to transform this vector from logic values to doubles (due in matlab). Finally we will make a “for loop” that give us a peak when the actual value (i) is one and the next one (i+1) is zero in the derivative vector as we show below. The resultant vector when we do a derivative is going to have a length (i-1) that the length of the principal vector. In order to fix this problem we might add (i+1) to the resultant vector. Figure 2 shows how we can show this result.
Finally, we can make use of the PeakUtils library to select the peaks we need according to a distance between them. The result of this will be as shown in figure 3.