Is it possible to generate a square wave from an audio?

Hi, I would like to generate a square wave (a wave that has 0 or 1 values only as shown in the attached image). The small signals that have low volume (less than a specific value or threshold value) should generate 0 volume output. The big signals that have higher volume (higher than the threshold value) should generate max volume output ( output value = 1). Is it possible to do it with audacity? Audacity is a great software and I’m sure it can do it.

Thank you so much,
01 square wave.png

Try applying this code in the Nyquist Prompt effect (see:

;version 4

(setf threshold 0.2)

;; Time resolution in seconds
(setf time-res 0.1)

(defun lowrate (sig step)
  (when (arrayp sig)
    (setf sig (mix-to-mono sig)))
  (snd-avg sig step step op-peak))

(defun mix-to-mono (sig)
  (mult 0.5 (sum (aref sig 0)
                 (aref sig 1))))

(setf step (truncate (* *sound-srate* time-res)))

(let ((lo-rate (lowrate *track* step))
      (bignum (power 2 32)))
  (setf lo-rate (sum lo-rate (- threshold)))
  (setf *track* (force-srate *sound-srate* lo-rate))
  (mult bignum (clip (s-max *track* 0) (/ bignum))))

Thank you so much for giving me this code. One small issue, I don’t really know how to use it. Where can I inter or insert my threshold value? I tried it as is, and I got this message but the waveform didn’t change to square wave. What can I do?

I also tried to debug and I received this error:

error: unbound variable - TRACK
if continued: try evaluating symbol again
Function: #<FSubr-LET: #1b182d0>

Good move trying the debug button. :ugeek:

If you are using the current version of Audacity, you either missed off the first line, or there was a space at the beginning of the first line.
The first line should be exactly:

;version 4

(with no space at the start)

In addition there was a typo in my original code that caused a different failure, but for stereo tracks only. I’ve fixed that in the previous post.

I would like to generate a square wave

Since that’s not audio, scientists and engineers are warned that in a race between sound presentation and scientific rigor, sound is going to win. For one example, during “perfect quality” WAV output, a tiny amount of carefully generated noise is added to the signal to reduce sound distortion in Audacity signal management. You should expect that because it can mess up signal assumptions.

In your particular case, you may or may not know that square waves are made up of a lot of sine waves carefully managed so their edges line up. Except, to get nice clean square waves—good rise and fall times—you need a lot of sine waves and you don’t have a lot of sine waves. 44100 sampling rate only gives you scientifically pure sine waves out to about 17,000Hz and it fudges everything else. You can’t hear the fudging when applied to sound, but it can really mess up square wave edges.

Similarly, getting square waves into and out of a computer can be a challenge because soundcards intentionally limit high frequency fidelity. That’s not good news for a wave type that depends on high frequency fidelity for its existence.

So why do we need square waves? What’s the goal?


It works!!!

Thank you so much Steve for your great work. It’s an amazing code. I really like it. It’s simple to use. You solved my issue. Thanks again man. God bless you and your family.
If you’re a developer or a programmer, I would like to give you my feedback or experiment: After you editing the code, It works on the current version only but that’s okay. I have an older version (Audacity 2.0.5) where it gives the same error.
I appreciate your replies and your time that you spend with me. I really like people who think out of the box and invent things. In similar situations, people tell me that the feature doesn’t exist in some software so I can’t do it. But you did it for me even if Audacity doesn’t explicitly have a tool for it. Good luck and have a nice day.

It’s not really true that square waves are made up of a lot of sine waves. At least, it doesn’t happen practically. From my electrical engineering background, we just make square waves out of sine waves to better understand them and because we love playing with mathematics. It’s how our “brain” understands things. But in the practical life, Computers and electroncs work using square waves (and the reason is because they are made of transistors that work like switches). Sine waves are too complex to implement for a device made of transistor. A square wave means electricity is on or of. Nothing else. So it’s simple to implement. So I don’t agree with the first part of your reply with all my respect :slight_smile:

What’s my goal?
That’s a great question. I love making 3D graphic characters and I would like to the movement of the cartoon character mouth to mach the speech. I found out that the most effective vowels are A, E and O. So, I’ll use the square wave to turn on or off those vowels and other characters. This way, I can automatically generate the motion of the mouth without having to animate each word individually. If you have better solution, let me know please.

Thank you for your reply,


Thank you, I’ll compare both solutions. God bless you,

Computers and electroncs work using square waves

True, as long as you stick in the digital or mechanical domains. Audacity is an audio editor and works with sound restrictions. Hence the question about the goal.

There are crossovers. There is a microphone distortion we called Frying Mosquitoes where USB digital transmission leaks into the sound and creates a festival of interfering sine waves along with your voice.

We get questions about jobs and goals that Audacity is just never going to achieve because it’s the wrong tool. It’s good to check.


Thanks for sharing this code! I wanted to try this on a high def sine wave but when the script ran
no square waves were produced - just reduced to a flat line on the HUD, is that normal?

Yes, that’s expected for a constant tone. See the post that I was replying to.