# Re: [music-dsp] Playing a Square Wave

```>  A bandlimited squarewave of 8 kHz @ 44.1 kHz samplerate is a sinewave. 3
* 8 kHz is already outside of the bandwidth.```
```
Well the example used was Middle C, which is 261.6 Hz. And would have 75 or
so harmonics before reaching the limit.

> This means that the basic frequency must be pretty low to get a square
wave shape.

Yeah, but that's exactly what I mean. At any frequency, you lose the upper
harmonics that make it perfectly square. So at what point do we cross the
threshold of it being square enough, or near-perfect enough. When is it
considered a square or not?

If you record that 8khz square and your result is a signal with 3
harmonics, did you not actually record a square? Record a 1hz square, it
will look square... until you zoom in far enough. You'll still get some
very tiny ripple because you only keep the highest 44k harmonics and not
all infinity harmonics. And if you don't get a ripple, you're probably
aliasing.

So my point is I think it's incorrect to assume it needs to look like a
square shape to be considered a square wave. Because no matter what, if you
zoom in enough a correct square stops looking like a square.

We can try to debate the threshold at which is looks or sounds squarey
enough, but that's more of a philosophical debate than a technical one.

Neil

On Wed, Jun 13, 2018 at 4:58 PM Uli Brueggemann <uli.brueggem...@gmail.com>
wrote:

> 75th or 150th harmonics?
> A bandlimited squarewave of 8 kHz @ 44.1 kHz samplerate is a sinewave. 3 *
> 8 kHz is already outside of the bandwidth.
> This means that the basic frequency must be pretty low to get a square
> wave shape.
>
> - Uli
>
> 2018-06-13 20:40 GMT+02:00 robert bristow-johnson <
> r...@audioimagination.com>:
>
>>
>>
>> ---------------------------- Original Message ----------------------------
>> Subject: Re: [music-dsp] Playing a Square Wave
>> From: "Neil Goldman" <neilgoldman...@gmail.com>
>> Date: Wed, June 13, 2018 11:16 am
>> To: ra...@raito.com
>> music-dsp@music.columbia.edu
>> --------------------------------------------------------------------------
>>
>> >> such a simple wave like the square wave, just two signal levels with a
>> >> near instantaneous jump between them
>> >
>> > I think I disagree with this definition of a square wave. This is what a
>> > perfect, ideal one would look like, but even in reality I don't think
>> any
>> > system (digital or analog) can exactly produce a perfect square and the
>> > infinite bandwidth it takes for the infinite number of harmonics.
>> >
>> > Even assuming a magically perfect and noiseless analog square wave
>> > generator, at the very least your speaker cones can't teleport between
>> two
>> > positions. And I'm no electrical engineer but the circuitry must also
>> have
>> > some kind of damping effect on the extreme ranges? I mean even the
>> > infinitesimally faint harmonics that will exist in the Mhz range, Ghz
>> > range, etc up to infinity.
>> >
>> > If you remove or dampen any of those higher harmonics, even ones well
>> > beyond the range of human hearing, this perfect square shape takes on a
>> bit
>> > of a ripple shape. At what point is it no longer a "true" square wave?
>> >
>> > So I would argue that a perfect square wave doesn't exist anywhere
>> except
>> > in theory, and it's more useful to define it by its harmonic series.
>> And at
>> > that point it doesn't look easier or more complex than any other common
>> > waveshape.
>>
>>
>> Neil here hits it pretty much spot on.  Theo's equipment and Theo's
>> hearing does not have infinite bandwidth.  i sorta doubt that he would be
>> able to discern the difference between a "perfect" square wave and the same
>> waveform passed through a nearly perfect brickwall filter with the upper
>> edge at 20 kHz.  they'll sound the same.
>>
>> so then the task to generate this square wave digitally is to generate a
>> bandlimited representation keeping all of the harmonics up to our limit of
>> hearing.  for a square wave at middle C, you need to break it down to
>> harmonics (say, using Fourier Series), keep all of the harmonics up to the
>> 75th harmonic (about 20 kHz), ditch the rest, and then digitally reproduce
>> that waveform.
>>
>> there is BLIT (or integrating a BLIT to get a square wave), but those
>> BLITs are stored in some kinda wavetable, and i don't see why not just
>> represent the bandlimited waveform itself in a wavetable.
>>
>> so using an FFT of a large size.  let's say N=64K points.  in that 64K
>> buffer, draw out a single cycle of the analog waveform you seek; square,
>> saw, triangle, PWM, sync-saw, sync-square, or even a single cycle of a
>> recorded waveform.
>>
>> FFT the bastard.  bin 0 will have DC.  bin 1 and bin N-1 will have the
>> amplitude and phase of the 1st harmonic.  bins 2 and N-2 have the 2nd
>> harmonic (the evens should be zero for a square wave), etc.
>>
>> then for middle C, go up to the 76th bin and zero all of them up to the
>> (N-76)th bin.  that will bandlimit your waveform.
>>
>> then inverse FFT the thing.
>>
>> the waveform you have left in 64K points is a bandlimited square wave
>> that will sound just fine for around middle C.  you can reduce the size of
>> that waveform by decimating (or down-sampling) down to about a 256-point
>> waveform for storage.  but for wavetable synth playback, you really want
>> that waveform represented with more points than 256.  i would recommend
>> 2048 or 4096 points.
>>
>> then you have to do this again for square waves at other pitches.  say
>> one octave lower, then you need to keep 150 harmonics.  or an octave higher
>> than middle C, you need to bandlimit it to 37 harmonics.
>>
>> then, in keeping with wavetable synthesis, you keep all of these
>> bandlimited square waves (say 2048 points per waveform) in memory and you
>> mix them proportionately as the note goes up and down the keyboard.  i
>> think that two waveforms per octave is generally good enough.  but maybe
>> you want 3 or 4.
>>
>> so BLIT, wavetable, or some other mathematical algorithm to generate a
>> bandlimited square on the fly (i did something like that for Kurzweil, but
>> i ain't saying what it was, and i don't recommend it anyway).  that's how
>> you do these things.
>>
>>
>>
>> --
>>
>>
>> r b-j                         r...@audioimagination.com
>>
>> "Imagination is more important than knowledge."
>>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> dupswapdrop: music-dsp mailing list
>> music-dsp@music.columbia.edu
>> https://lists.columbia.edu/mailman/listinfo/music-dsp
>>
>
> _______________________________________________
> dupswapdrop: music-dsp mailing list
> music-dsp@music.columbia.edu
> https://lists.columbia.edu/mailman/listinfo/music-dsp
```
```_______________________________________________
dupswapdrop: music-dsp mailing list
music-dsp@music.columbia.edu
https://lists.columbia.edu/mailman/listinfo/music-dsp```