Daqarta
Data AcQuisition And Real-Time Analysis
Scope - Spectrum - Spectrogram - Signal Generator
Software for Windows
The following is from the Daqarta Help system:

# Square Wave

Controls: Gen Dlg >> Stream >> Wave >> Square
Macro: Wave=Square

## Introduction:

This button activates a standard bipolar square wave, and also opens a separate dialog allowing you to select Snap or Interpolate edges.

To create a unipolar square wave, set Level to 50% and Offset to 50%.

This wave has a fixed 50% duty cycle; use the Pulse wave to create other duty cycles (rectangular waves).

## Edges - Snap vs. Interpolate:

To understand the need for the Snap/Interpolate Edges selection, consider that the output of a sound card is made up of discrete samples that come at a uniform sample rate. A perfect square wave at 100% Level would consist of a half-cycle of samples at +100% (a digital value of 32767), followed by an equal number of samples at -100% (-32767). There would be no intermediate values, so the wave would "snap" from one extreme to the other; a rising edge goes from -32767 on one sample to +32767 on the next. The opposite is true for falling edges. (This applies to the digital waveform; your sound card's output filters will smooth the analog transitions somewhat.)

To produce a perfect square wave of a specified frequency, each cycle must have an equal number of positive and negative samples. This would seem to limit the possible square waves to those with N high and N low samples per cycle, where N is an integer due to the fixed sample rate. (The output can't change between sample times.)

The possible output frequencies would thus be SampleRate / (2 * N). Using the standard sample rate of 48000 Hz with N = 1, we'd get a frequency of 48000 / (2 * 1) = 24000 Hz. Increasing N would give 12000 Hz, 8000 Hz, 6000 Hz, 4800 Hz, etc, with no values in between. This would give good frequency resolution only at very low frequencies, such as N = 48000 for 1.000000 Hz and N = 48001 for 0.999979 Hz.

But although N must be an integer, we can change its value in real time, including between half-cycles, such that the average has a fractional part that allows any desired frequency. Snap mode does this by keeping track of when the rising and falling edges should ideally happen for the specified frequency, and always chooses the closest sample. This maintains the sharpest possible edge transitions, but at the expense of a certain amount of jitter in the exact position of those edges due to varying N.

Interpolate mode uses softer edge transitions to reduce jitter. For example, if the rising transition from -100% to +100% is computed to ideally arrive halfway between the current sample and the next, the interpolated output value will be 0... halfway between -100% and +100%. If the ideal edge is only a quarter of the way to the next sample, the edge value would be +50%, and so on.

To see the difference visually, load the Default.GEN Generator setup and toggle it on. (You may want to reduce or mute the output; hit the F9 key to open the volume control dialog.)

Now click the Left Waveform Controls button to open the Left Stream 0 dialog and set Tone Freq to 1000.2 Hz. (Note that 1000.0 Hz would give a 'perfect' square wave with N=24 in the SampleRate / (2 * N) formula, so it wouldn't show the jitter problem.)

Click on the Wave button and set the type to Square, which will open the Snap / Interpolate dialog with the default Snap mode active. With X-Axis eXpand off, you'll see the edges of the square jittering from side to side. The amount is small, only an occasional single sample, so it's not noticeable in a simple screen capture. The image below is a superposition of two captures, one in green and one in red, that shows the extent of the jitter. It appears to be mostly just alternating green and red lines because without X-Axis eXpand, two lines that are a single sample apart are less than one pixel apart here.

Next, set eXpand to cover 4.8 to 5.2 msec. Each sample period is 20.833 microsecond (1/48000 Hz), which is about 40% of a major X division of 50 microseconds. Daqarta "connects the dots" to show a continuous plot, so an "instantaneous" snap appears as a sloping line from -100% to +100%. With two traces (both green) superimposed below, you can see that the entire edge shifts by one sample, between starting or ending on 5.00 msec. Note that this is where we would expect to see a stable edge if the frequency was exactly 1000.0 Hz.

Now toggle from Snap to Interpolate. With eXpand off you see stable positions of the peaks, but there is a small jitter around the zero crossings. Unlike Snap mode, which had only vertical edges, here there are many intermediate zero-crossing points. You can see them as a changing pattern of tiny offsets of the center section of the edges, where the height of that offset varies over time. This is evident on a live display, but can still be seen faintly on a screen capture. To make it more obvious, below we show a superposition of green and red traces, as previously shown for Snap mode:

With eXpand active you can see varying 'kinks' at the edge, instead of the shifting edge seen for Snap. Below we show two of the many constantly-changing kink positions for the same edge:

## Jitter-Free Square Waves - Burst Method:

You can make jitter-free square (or other rectangular) waves using Burst. The idea is to apply Burst to a constant DC level, such that the burst envelope itself becomes the output waveform. By setting Rise and Fall to 0, the edges will be perfectly sharp. By setting Cycle to be exactly twice the High duration, the wave will be perfectly square.

Start by loading the Default.GEN Generator setup to get a known starting point. Use the F9 key to open the volume control dialog, and mute the output before toggling the Generator on.

Leave the Wave type set to Sine, then set Tone Freq to 0, which will give a flat (DC) line at 0 volts because the sine of zero degrees is zero. Now set Phase to 90 degrees, and the DC level will jump to maximum, which is the default 100% Level setting.

Click on the Burst button to open its dialog, then toggle Burst On. The display will roll until you toggle Train Sync on as well. You'll see a very strange 'rounded gear tooth' shape due to the default Burst settings.

Now set Rise and Fall to 0. Leave the High value at 128 sample, and Cycle at 256, and you will see a perfect unipolar square wave. (There may appear to be kinks in the edges due to undithered graphics, but these will vanish if you use X-Axis eXpand.)

If you set Offset to -50% while Level is at 100%, you get a bipolar square wave between +/-50% of full scale.

One drawback to the Burst method is that it only allows certain frequencies... the same SampleRate / (2 * N) values as with the normal Square wave. It's also rather awkward to change frequencies, since you have to set both Cycle and High values. Of course, you also have to compute the values to set, namely High = SampleRate / (2 * F), where High is in samples and F is the desired frequency in Hz. Then you double this value to get the Cycle setting. (You can also set the Burst units from Smpls to sec and use High = 1 / (2 * F).)

Despite these inconveniences, this is the best approach when you want to set a particular fixed-frequency square wave, especially at extremely low frequencies. For example, if you want the wave to have a 3 second period, using the normal Square wave would require entering a value of one-third Hz as the Tone Frequency. The resolution of the control is 5 decimal places, and the conversion from decimal to binary and back results in 0.33331 Hz. Using the Frequency Counter in msec mode, this gives a period of 3000.217 msec. The next-higher Tone Frequency you can set is 0.33340, which gives 2999.413 msec.

But using the Burst approach you compute High = 72000 and Cycle = 144000 samples and you get a period of exactly 3 seconds, verified using the Frequency Counter with up to the maximum of 9 decimal places.

Curiously, you can also use the Burst method to get precision sine waves at ultra-low frequencies. Set Burst Shape to 1 (pure cosine), then set High to 0 and set Rise and Fall each to half of the desired Cycle.

GO:

We respond to ALL inquiries, typically within 24 hrs.
INTERSTELLAR RESEARCH:
Over 30 Years of Innovative Instrumentation