# Differentiating between a square wave or sawtooth wave with a circuit…?

I want to build a simple sensor that takes a 100 kHz square or sawtooth wave with a known amplitude and outputs a high if it receives a square wave or a low for sawtooth.

I'm pretty sure this requires some sort of comparator, but I'm not sure how to approach this problem myself. Could anyone outline some sort of approach? (I'd like to figure out the details).

Thank you in advance!

-
what's the output when the input is neither, e.g. a sine wave? – JustJeff Sep 29 '11 at 2:41
Are all signal levels at or above ground? You do mean sawtooth (falls faster than rises) and not triangle.? – Russell McMahon Sep 29 '11 at 7:24
sort of related: stackoverflow.com/questions/1141342/… – endolith Sep 30 '11 at 21:52

If the frequency for both waves is going to be 100 kHz with the same amplitude, you could construct a narrow bandpass filter at 200 kHz to put the signal through. In theory a pure squarewave should only have odd harmonics, so there should not be much output at the second harmonic frequency. On the other hand, a sawtooth wave has booth even and odd harmonics, so you will get a greater output. The peak amplitude for the second harmonic of a sawtooth wave will simply be $\frac{2A}{\pi}$, where A is the peak amplitude of the input sawtooth. If wish you can then follow up the output of the bandpass filter with a peak detector and some kind of comparator.

-
Such a bandpass filter would need to have steep sides in order to get any useful data out of it. Something with a slope of 12dB/octave would be a good start. 3dB/octave would not adequately filter out the fundamental. – David Kessner Sep 29 '11 at 15:24
It might be adequate if you ran a comparison of the filter output to its input. – Chris Stratton Oct 6 '11 at 17:49

An outline of a solution: Maybe run it through a differentiator. The derivative of a square wave will be alternating positive-going and negative-going spikes, whereas the derivative of a sawtooth should be more or less constant at a low value in one polarity during the rampy bits, with periodic larger valued spikes in the opposite polarity when the sawtooth resets. So then HPF that to get rid of the constant low-values you get from the sawtooth ramps, and look to see whether you're getting spikes of both polarities, or just a single polarity.

-

There is no one "right" answer for this, since it really depends on the ability of the person designing the circuit to build it correctly. Some approaches are more difficult than others.

Since I have a background in audio, I would use an audio based approach. I would rely on something called the "crest factor". The crest factor is, basically, the difference between the RMS and the Peak level. So if you made two "VU Meters", one that measured the peak value and another that measured the RMS value and compared the difference then you could fairly accurately tell the difference between a square wave and a sawtooth.

For a square wave, the RMS and Peak levels will be identical. For a triangle wave the RMS level will be 4.77 dB lower than the peak. A sawtooth wave will be similar to a triangle wave, but I don't have the exact number handy.

-

You can easily detect some simple waveforms by detecting the flanks of the signal. A square has quick rising and falling flanks, a sawtooth has only quick rising or quick falling flanks, depending on the signal.

So you check for rising and falling flanks: if you detect both, it is square. If you detect only one type, it is triangle, as long as you are sure only these signals will be input.

Try with a differentiator circuit, which is easily done with an opamp. See here: http://www.physics.iitm.ac.in/courses_files/courses/eleclab03_odd/mathematical_operations.htm

The steepness of the flank is represented in the output of the differentiator.

Feed this signal and its inversion into Schmitt-Triggers and / or retriggerable monoflops, and you have logic level representation of RisingFlank and FallingFlank, which in turn you can use for further computation or display.

-

If one passes a square wave or sawtooth wave through a high-pass filter whose cutoff frequency is far above the fundamental of the original wave, the output will either be an alternating sequence of positive and negative pulses (for a square wave), or else will only have pulses in one direction (for a sawtooth).

See this circuit.

-

Another simple solution for a fixed amplitude: Use a comparator to compare the signal against a 95% amplitude constant voltage. E.G. if the amplitude of the wave is 0v..1v, then compare it against 950mv.

A 50% duty cycle square wave will give you a 50% duty cycle square wave out. A saw tooth wave will give you a 5% duty cycle square wave out. You can use a microcontroller to detect this on a cycle by cycle basis.

-

If the signal has a fixed amplitude, then you can run the signal through a low pass filter (average the signal) and compare the average values. The details on the duty cycles will determine which average value is higher. If, however, the square wave is 50% duty cycle and the triangle wave is 100%, then the average will be equal, and you'll have to explore a more complicated solution.

-
 A half-wave rectifier would allow this to work for the duty cycles you mentioned. – Kevin Vermeer Oct 4 '11 at 19:51