The attached graph below consists of $4.5$ seconds of pattern which I have recorded via a hardware device.

enter image description here

The $x$-axis is the timestamp value in seconds and the $y$-axis is the hardware value. The values are taken on $0.01$ seconds of interval.

As seen in the graph, there is a definite pattern/curve from $0.5$ to $3$ seconds. I tried calculating a definite value using the below steps:

  • Taking 2.5 seconds block starting from $0$ Blocks: $0 - 2.5$, $0.01 - 2.51$, $0.02 - 2.52$, etc.
  • Now divide this $2.5$ second block into $5$ parts of $0.5$ seconds interval blocks. $(2.5/5 = 0.5)$
  • So, first block of $0 - 2.5$ will give: $0, 0.5, 1.0, 1.5, 2.0, 2.5$
  • Now I have calculated the slope of these values $(x_2-x_1/y_2-y_1)$. This will give me $5$ slope values.
  • Lastly I have added these values, $(\textrm{Slope}_1 + \textrm{Slope}_2+\ldots + \textrm{Slope}_5)/5$ to get a single average value.

The thing is this value is definite for the time block of $0.5 - 3$ seconds (the curve which I want to identify), but sometimes it comes for other time blocks also.

Also, second issue is, In the current graph, most of the values start from near $0$ or at $0$ on the $y$-axis. In real time, values can start from $-1$ or $1$ also, but the curve will still be the same, I have tested it.

I think the only way is to calculate the value of the curve, as the $y$ values are not definite, but the curve shape is. In real time I would have around $30$ seconds of data and I need to check if any $2.5$ seconds from those $30$ seconds contains a curve like this.

  • $\begingroup$ Thanks for your elaborate problem description. What do you mean by "definite value" and "definite curve"? Do you mean that you look for this particular pattern of the curve in a larger signal portion? Do you need to identify, where and how often it occurs? Can you provide us the test data you plotted there online? $\endgroup$ – Maximilian Matthé Dec 20 '16 at 7:26
  • $\begingroup$ Thanks for the reply. Yes, " particular pattern of the curve in a larger signal portion" is what I am looking for. I require the same. Let me see If I can provide the test data. But for starters, it can be any data based on 0.01 interval seconds. Actually, I have 4 type curves which I need to identify. I was looking for a formula to somehow calculate the area/value of a pattern for a specified time block. $\endgroup$ – nr5 Dec 20 '16 at 8:08
  • $\begingroup$ From my understanding you are looking for some kind of pattern matching algorithm which will match the shape(and not the values) of a particular pattern in a time sequence. As you mentioned that y axis values might not be the same in new time sequence, is entire curve shifted across y axis or it is scaled(linear or non-linear) ?? because if its just shifted on y axis slope based methods will work, but if its scaled slope based methods might not be useful. $\endgroup$ – arpit jain Dec 20 '16 at 8:14

You can use cross-correlation to detect a known signal in a received/measured signal. Here is an example using python and numpy/scipy:

pattern = lambda t: 0.8*(abs(t-1) < 1).astype(int) * np.sin(-2*np.pi*t/2)
Fs = 100.0
t = np.arange(-1, 15, 1/Fs)
t_samp = np.arange(-0.5, 2, 1/Fs)

sigma = 0.02  # add some noise to the measurement

rx = -1+0.1*(t-3)+pattern(t) + pattern(t-4)+pattern(t-9) + np.sqrt(sigma) * np.random.randn(len(t))
patsamples = pattern(t_samp)

plt.plot(t, rx)
plt.title('The received signal')

plt.plot(t_samp, patsamples)
plt.title('The pattern you look for')

metric = np.correlate(rx, patsamples, 'same')
plt.plot(t, metric)
plt.title('The correlation metric')

peaks = scipy.signal.find_peaks_cwt(-metric, np.array([2.5])/Fs, min_snr=0.5)
peaks = np.array(peaks)
peaks = peaks[metric[peaks]>40]
plt.stem(t[peaks], metric[peaks])

enter image description here

As you see, this works also when there is an offset or a constant slope in the received signal. In the correlation metric you see peaks, which you need to detect. There is the scipy.signal function find_peaks_cwt, however it is not very intuitively usable. For better/easier implementation of find_peaks you can have a look at this page.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.