Main Content

# Peak Analysis of Data

This example shows how to perform basic peak analysis of data in your ThingSpeak™ channel. You learn how to calculate the peaks in your data.

### Read Data from the Car Counter ThingSpeak Channel

The car counter ThingSpeak channel uses a Raspberry Pi™ and a webcam to count cars on a busy highway. A car-counting algorithm is running on the Raspberry Pi, and the density of cars counted every 15 seconds is sent to ThingSpeak. Use the `thingSpeakRead` function to read the last 60 data points for eastbound traffic from channel 38629.

```data = thingSpeakRead(38629,'NumPoints',60,'Fields',1,'outputFormat','table'); ```

### Find Peaks

The highway often gets backed up due to rush hour traffic or traffic incidents. Therefore, traffic backup is indicated on the highway by peaks in the density of cars counted every 15 seconds. A general rule for drivers is to follow the three-second rule with respect to the car ahead of them. Calculate the number of times at which the density of cars was more than 15 per second over the last 60 data points.

```[peakValues, peakLocations] = findpeaks(data.DensityOfWestboundCars,data.Timestamps,'MinPeakHeight',10); numOccurences = length(peakValues); disp(numOccurences); ```
``` 11 ```

### Send Number of Peak Occurrences to ThingSpeak

Send the `numOccurences` value to a ThingSpeak channel using the `thingSpeakWrite` function. Change the `channelID` and the `writeAPIKey` to send data to your channel.

```channelID=17504; writeAPIKey='23ZLGOBBU9TWHG2H'; thingSpeakWrite(channelID,numOccurences,'WriteKey',writeAPIKey); ```