Most of the sparks you see coming from a train in a dark tunnel are the result of a subway car's contact plate losing contact with one section of powered rail and picking it up on the next. The powered rail becomes vertically thinner as it comes to its end, and goes from thinner to thicker as it starts again. Sometimes the shoe grates against the beginning of the rail as it makes contact with it, causing a spark.

This happens most frequently on switches because of two factors - first because there's no way to run a continuous third rail along a switch (the rail would cut diagonally along the running rails) necessitating a break in the powered rail, and secondly because the train is making contact with the new third rail at an angle instead of straight on.

This is also why the sparks you see are periodic - it happens every time a new car passes the point where the new third rail starts.