Message Boards Message Boards

Train Detector - Where to Go Next?

Posted 9 years ago

I built a train detector (OK, I hooked up a microphone to a Raspberry Pi 2). I record CD quality sound (44.1 kHz) to a sound file and run it through a C program to perform a FFT. You can then easily detect the signature of train whistles from the frequency spectrum. I then record and display the data using a conventional Spring Boot / MySQL application. All of this is running on a Raspberry Pi 2 with a 32 GB sim card.

You can see it working at: Train Counter

Code is at:

I've gotten as far as recording the time between trains, and now I want to predict when the next train will come. I think this is a classic statistics problem - "time between events" and "predict the probability of the next event". Any ideas on how to proceed?

Sample data are attached.

enter image description here

POSTED BY: George Campbell
14 Replies

We live close enough to several public crossings on a busy line to hear the warning signals (Morse Code 'W' - long, long, short, long) as the trains approach and enter the level-crossings.

I observe each engineer has a 'style' involving the cadence and sound level. It's not as pronounced as with steam whistles (where tones change with the amount of steam flowing through the whistles), but I'm sure you could determine which engineer is driving each train.

I'd be interested if this could be done on a Pi by learning the various "fists" (name from Morse Code world).


POSTED BY: Glenn Peterson

The Pi is sampling at 44 kHz, so it has the resolution to detect the length and spacing of each individual horn blast. Pattern matching would then be a matter of software.

POSTED BY: George Campbell

Maybe you could use the new machine learning functions as statistical prediction is one of the things they are good for and your data easily gives them enough to train with.

George I have two questions:

  • Where is R-Pi mounted and what is a setup? Maybe you could post a photo?

  • Do you rewrite your data and how much data you already have?

POSTED BY: Vitaliy Kaurov

The pi is mounted in my rec-room. A long microphone cable snakes up into the attic. The microphone itself sits in the space offered by the aluminum ridge vent of the roof. I'll take some pictures next time I'm up there.

POSTED BY: George Campbell

"Are you aware that Wolfram Language runs on Raspberry Pi and is available there for free?" - I am going to load this when I get a chance. For one thing, I'd like to use the FFT available from Wolfram and ditch the custom C library. Secondly, I'd like to make use of the statistics tools. So I have some homework to do!

POSTED BY: George Campbell

Sounds like a fun project, it's definitely worth downloading Mathematica! You can fit your histogram to various distributions (and find the best fit), calculate the expected time between trains, probability of a train coming the next x minutes (based on time from last train) etc. If the noise of axles going thru gaps in the tracks is loud enough, you might even be able to count the # of axles of each train and estimate tonnage of cargo going by your house - could be a good economic indicator, or you could get the speed of the train if you know axle separation (by finding the auto correlation) :)

POSTED BY: Kay Herbert

How did I connect the microphone? I give some hardware details in the "C" file comments on Github:

Thanks for looking at all this.

POSTED BY: George Campbell

Hi everyone - thanks for the interest.

This is a freight line, so the trains do not run on a schedule. Instead, demand, crew availability and line capacity all play a complex role. And yet the distribution of minutes between trains seems to fall into a Poisson Curve shape (I do remember my sophomore stats a little bit).

Raw data:

These are timestamps representing a minute wherein a train horn was heard (what if 2 trains come at the same time? Counts as one so far as the rPi can tell - but doesn't happen often). Since data and time are unwieldy, I included the posix time column, which is the seconds since 1/1/1970. It is useful for calculating differences.

Moderators' note: Data also attached to this post below and to the top original post.

POSTED BY: George Campbell

George a very cool project, indeed! Are you aware that Wolfram Language runs on Raspberry Pi and is available there for free. There is a whole group here dedicated to applications on R-Pi and Wolfram.

But do not trains run by schedule? Are you trying to gather stats on how late they are? Or are these cargo trains?

POSTED BY: Sam Carrettie

George, this is a very nice project you got going. For those who would like to take a stab at modeling. I extracted the main info I think - the time series of differences in seconds between the trains. Data are below - simply copy and paste in the notebook for modeling.

data = Import["train.csv", "CSV"];
times = data[[3 ;;, 3]];
diff = Differences[times]


ListLinePlot[diff, PlotTheme -> "Scientific", AspectRatio -> 1/5, 
 ImageSize -> 900, PlotRange -> All, Filling -> Bottom]

enter image description here

POSTED BY: Vitaliy Kaurov

A first interesting observation. We have an interesting and for now experimental function FindDistribution When it ran on various parts of data it suggests that data are distributed according to WeibullDistribution or LogNormalDistribution distributions. And guess what? This are the usual suspects for wind speed distributions, for example see:

Mixture probability distribution functions to model wind speed distributions

Please do not post of issues with FindDistribution here - it is marked EXPERIMENTAL in documentation and under development

Still FindDistribution works very nicely. Some insights are below. The data diff are taken from the post above.

head = FindDistribution[diff[[;; 400]]]

WeibullDistribution[0.952215, 1868.28, 180.648]

tail = FindDistribution[diff[[400 ;;]]]

LogNormalDistribution[7.10678, 0.923829]

his = Histogram[diff, 60, "ProbabilityDensity", 
   ChartElementFunction -> "GradientScaleRectangle", PlotTheme -> "Detailed"];

plo = Plot[{PDF[head, x], PDF[tail, x]}, {x, 0, 10000}, 
   PlotStyle -> {Directive[Thick, Red], Directive[Thick, Black]}, 
   PlotRange -> All, PlotTheme -> "Detailed"];

Show[his, plo, ImageSize -> 700, PlotRange -> {{1, 10000}, All}]

enter image description here

POSTED BY: Vitaliy Kaurov

Wow - now there's a connection I would have never made! I read up on Weibull Distributions on Wikipedia and it does mention that they are of use in industrial engineering for "manufacturing and delivery times". I should be able to calculate some probabilities based on the curve parameters, such as "The probability of a train appearing in the next 10 minutes." - I think there's an iPhone App idea if that could be made to work. Thanks for processing the data - it is fascinating!

POSTED BY: George Campbell

Maybe you could do this with Predict[] given day of week and time of day as input, assuming trains run on some kind of schedule? Otherwise maybe intervals (time since last train) for night/day/weekend?

BTW your detector is cool. Could you share how you connected a microphone to the RPI?

POSTED BY: Kay Herbert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract