Is it possible to generate a real square wave?

Discussion in 'Working with Sound' started by SyNtH., Jan 28, 2017.

  1. SyNtH.

    SyNtH. Platinum Record

    Joined:
    Nov 27, 2013
    Messages:
    402
    Likes Received:
    229
    Ive currently setup a testbed to see what was going on with square wave generation. I used the vanilla square wave from 3xOsc , and when recording square wave into edison i got this effect when zooming in? Can anyone describe why this is happening?


    Square Recording.png

    It seems to happen with serum and most likely other synths. I tried it with and without HQ oversampling as well. Any hints? Im assuming its aliasing or something. I read up on wiki about square waves and this came up:

    https://en.wikipedia.org/wiki/Square_wave

    Characteristics of imperfect square waves
    As already mentioned, an ideal square wave has instantaneous transitions between the high and low levels. In practice, this is never achieved because of physical limitations of the system that generates the waveform. The times taken for the signal to rise from the low level to the high level and back again are called the rise time and the fall time respectively.

    Surely the physical limitations can be overcome in the digital realm? Was the generation of the square optimized for being fast at the cost of precision? I know most of the old FL plugs were made with the help of a DSP "Cookbook" which is why i think it could be that. Could an ADC not convert the signal if it was a theoretically correct square? Does the sample rate have to be super high in order for a square to be "correct"? Any hints would be helpful.

    I just saw at the bottom of the wiki for the square wave for the gif on the bottom right that it was made using additive synthesis and it gives the exact same effect as what i see in edison. Its weird because looking at the help docs for FL, 3xOsc is a subtractive synthesizer, would the effect be different or the same? Is it all just sinewave reconstruction/emulation to make other wavetables if a real square cannot be made?

    Cheers in advance.
     
    Last edited: Jan 28, 2017
    • Interesting Interesting x 2
    • List
  2.  
  3. DarthFader

    DarthFader Audiosexual

    Joined:
    Sep 28, 2014
    Messages:
    890
    Likes Received:
    754
    Location:
    Atlantis
    Possibly not within the restrictions of a consumer-grade electronic device.

    If you visualize the perfect square wave as a sum of the harmonics of an infinite number of pure sinewaves, you'll see where the softsynth can't quite get there. Infinity is a bummer.

    OTOH, a true square wave's harmonics would create a magnetic pulse of sufficient intensity to interfere with nearby electrical components. So, a small amount of rise and decay time error plus maybe a little ripple at the peak values is a reasonable tradeoff.
     
    • Like Like x 3
    • Agree Agree x 2
    • Interesting Interesting x 2
    • List
  4. Pinkman

    Pinkman Audiosexual

    Joined:
    Apr 22, 2016
    Messages:
    2,093
    Likes Received:
    1,944
    A "perfect" saw wave is near impossible to achieve in the digital domain because it would require an infinite sampling rate.

    Increasing the sampling rate will help with aliasing and reduce the "jitter"

    22000 kHz
    upload_2017-1-27_17-14-39.png

    192000
    upload_2017-1-27_17-16-15.png

    If you're up for it, here's an article on a technique called EPTR (Efficient Polynomial Transition Region) that can produce near-perfect waves without melting your PC. The internal code for the oscillator is provided in case you want to build it in Max/MSP
     
    • Like x 7
    • Interesting x 3
    • Agree x 1
    • Winner x 1
    • Useful x 1
    • List
  5. DarthFader

    DarthFader Audiosexual

    Joined:
    Sep 28, 2014
    Messages:
    890
    Likes Received:
    754
    Location:
    Atlantis
    :goodpost:

    Wow! Great source material @Pinkman - I learned something new today about EPTR from reading the article. Gratitudes, man. :)
     
  6. Andrew

    Andrew AudioSEX Maestro

    Joined:
    Oct 24, 2011
    Messages:
    1,980
    Likes Received:
    1,201
    Location:
    Between worlds
    Simple, here's 1kHz:
    Code:
    http://www41.zippyshare.com/v/DozUDADD/file.html
    

    Code:
    int(2*t*f)%2*2-1
     
  7. procrastinator2000

    procrastinator2000 Ultrasonic

    Joined:
    Apr 12, 2016
    Messages:
    24
    Likes Received:
    24
    in analog world, yes? i have no idea, but we perceive the sound you have there as squarewave, if the sampling rate would be infinity, it would be perfect square in theory, yes. edit: i guess what im trying to say is, digital or analog or whatever, to our ears it IS a perfect square wave, since all waveforms are actually made of sinewaves in real world, and our hearing is limited with 20khz upper limit. edit2: try to think of it like this, even if you have vst that creates a perfect squarewave, if you sample it to 16bit, 44,1khz "CD quality" wave file, it would look like that as in your picture afterwords (after the digital to analog conversion? can someone confirm or debunk?).

    [​IMG]
     
    Last edited: Jan 28, 2017
    • Like Like x 8
    • Interesting Interesting x 3
    • List
  8. GreatJobChamp

    GreatJobChamp Producer

    Joined:
    Dec 22, 2016
    Messages:
    286
    Likes Received:
    109
    isn't a bassoon a square or something?

    but yeah, I think it's all about adding odd multiples of sine to flatten out the peaks and troughs of the fundamental sine-tone...?? yeah?



    that is SOOOO cool man :wink:
     
  9. procrastinator2000

    procrastinator2000 Ultrasonic

    Joined:
    Apr 12, 2016
    Messages:
    24
    Likes Received:
    24
  10. tun

    tun Rock Star

    Joined:
    May 13, 2016
    Messages:
    1,179
    Likes Received:
    465
    in the digital world you can create almost a perfect square wave with a 1 sample crossing between 1 and -1, but it wouldnt matter anyway because it would be low passed before it comes out of the speakers. even if it wasnt the speaker would not be able to move fast enough to create it and you would in fact damage your speakers if you attempted it
     
    • Like Like x 2
    • Agree Agree x 1
    • List
  11. tulamide

    tulamide Audiosexual

    Joined:
    Feb 13, 2016
    Messages:
    847
    Likes Received:
    761
    This is the square wave osc I use in Flowstone. It was written in Assembler, so I have no clue about the algorithm, but I remember the keyword "polyBLEP" (polynomial bandlimited step). However, the cpu load of this single osc isn't measurable (= way below 0.1%) on my Notebook (i5-2540M)

    Square.PNG
     
  12. Yea, I knew that!
     
  13. procrastinator2000

    procrastinator2000 Ultrasonic

    Joined:
    Apr 12, 2016
    Messages:
    24
    Likes Received:
    24
    Although you cannot say it is jitter. It's ripples. Jitter is a (timebased) error. Ripples are not an error.
     
    • Like Like x 2
    • Agree Agree x 1
    • List
  14. fiction

    fiction Audiosexual

    Joined:
    Jun 21, 2011
    Messages:
    1,893
    Likes Received:
    688
    No problem creating a perfect square wave in the digital domain, like Andrew wrote.
    The thing is that to avoid aliasing, a more or less steep low pass filter has to be used in digital-to-analog conversion, removing spectral content that's inaudible anyway. The lower you set the LPF filter, the more "ripples" you'll see in the analog waveform.
    In the end it's designed to be like this and it doesn't matter because what you *hear* will still be a nice square wave.
     
    • Like Like x 1
    • Agree Agree x 1
    • List
  15. lpu2n

    lpu2n Producer

    Joined:
    Aug 11, 2015
    Messages:
    206
    Likes Received:
    121
    It's called the Gibbs phenomenon. Basically, since the rate of change (derivative) at the corner of a square wave is not a smooth function, it is impossible to represent it using a finite number of sine waves. The rate of change jumps from 0 instantaneously to infinity at the edge, and then back to 0 - and to get that result from a composite of sine waves, you need a sine wave component with an infinite slope. Of course, no such thing exists in nature.

    If you look close at the ripple, you see that it is smoothing out the corner with high frequency waves. As you add higher frequency sines (harmonics), those ripples become progressively smaller and less wide. It's pretty cool actually. Theoretically, you would need an infinite sample rate to reproduce a pure square.

    I wouldn't worry about it much though, as we can only hear up to 20kHz! With proper anti-aliasing the downside is minimal.
     
    Last edited: Jan 28, 2017
    • Like Like x 2
    • Interesting Interesting x 1
    • List
  16. tulamide

    tulamide Audiosexual

    Joined:
    Feb 13, 2016
    Messages:
    847
    Likes Received:
    761
    Now that's confusing. I understand all the details, yet the osc I use (see post above) is as close to a perfect square as I dream of. I never saw such a clean oscillation before. So it obviously is possible to reach it closely. Maybe it is similar to lightspeed, that you can come close to, but never reach? However, has anybody an oscillator that produces a cleaner square wave than the one I presented here?
     
  17. Aileron

    Aileron Audiosexual

    Joined:
    Jan 5, 2017
    Messages:
    523
    Likes Received:
    545
    Location:
    Merseyside
    Wow. There's nothing like learning from the Learned. Wonderful stuff.
     
  18. fiction

    fiction Audiosexual

    Joined:
    Jun 21, 2011
    Messages:
    1,893
    Likes Received:
    688
    Please read my post. Creating a 100% clean square wave in the digital domain is not the point, that's too easy.
    How have you measured your waveform? Certainly not with an oscilloscope on the output of your sound device.
    Or you've chosen the OSC frequency low enough to have an almost perfect square wave on the analog side.

    Try a 10 kHz square wave, re-record the sound from the analog output of your audio interface and watch what happens!
     
    • Like Like x 2
    • Winner Winner x 1
    • List
  19. Qrchack

    Qrchack Rock Star

    Joined:
    Dec 15, 2015
    Messages:
    797
    Likes Received:
    338
    Location:
    Poland
    If you just straight up create a perfect square (1 then -1) in the digital domain, you'll get an infinite number of harmonics above the fundamental. That means above (sample rate)/2 the harmonics will start to "bounce back", creating weird harmonics that are not related to the sound. This creates what's known as aliasing, and this is why most synthesizers remove everything above that limit. If they didn't, you'd get random inharmonic noises in your square.

    Now, for analog, it's totally impossible. No part will let you switch from 1 to -1 instantly. The best opamps and transistors will give you a switching time measured in μs (1/1000000 of a second), but it will never be an instant jump.

    So, for what it's worth, ideal square waves are either not possible (in analog), or not desirable/practical (in digital). Which means you should probably stop worrying about it.
     
    • Agree Agree x 1
    • Love it! Love it! x 1
    • List
  20. procrastinator2000

    procrastinator2000 Ultrasonic

    Joined:
    Apr 12, 2016
    Messages:
    24
    Likes Received:
    24
    so to summarize, to my understanding. a sine wave is the primary resonant frequency alone, sawtooth is the frequency plus all it's harmonics, squarewave is the frequency plus all the odd harmonics. so you would need immeasurable amount of the harmonics to have the squarewave to be perfectly square, but it does not matter anyway, since a perfect squarewave and a squarewave with it's harmonics capped at 20khz will sound identical to human ears. yes?
     
  21. tulamide

    tulamide Audiosexual

    Joined:
    Feb 13, 2016
    Messages:
    847
    Likes Received:
    761
    Ah, I see. I didn't get that last part. It wasn't about a square wave oscillator, but about how the waveform changes when transferred from the digital domain to the analog and back again. That's a completely different thing that I didn't notice. Sorry for that. Since digital is a finite domain and analog an infinite domain, there is much more to it than just the waveform that's sent through these. Loss of information (ADC) in the digital and artificial interpolated information (DAC) in the analog domain also have influence on the result.
     
Loading...
Loading...