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

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
ImUsingAudacity
Posts: 20
Joined: Sun Nov 20, 2016 7:09 pm
Operating System: Windows 10

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

Post by ImUsingAudacity » Thu Feb 18, 2021 9:11 am

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,
Attachments
01 square wave.png
01 square wave.png (22.8 KiB) Viewed 933 times

steve
Site Admin
Posts: 81227
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

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

Post by steve » Thu Feb 18, 2021 10:25 am

Try applying this code in the Nyquist Prompt effect (see: https://manual.audacityteam.org/man/nyquist_prompt.html)

Code: Select all

;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))))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ImUsingAudacity
Posts: 20
Joined: Sun Nov 20, 2016 7:09 pm
Operating System: Windows 10

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

Post by ImUsingAudacity » Thu Feb 18, 2021 10:55 am

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?
Attachments
0JPG.JPG
0JPG.JPG (74.25 KiB) Viewed 918 times

ImUsingAudacity
Posts: 20
Joined: Sun Nov 20, 2016 7:09 pm
Operating System: Windows 10

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

Post by ImUsingAudacity » Thu Feb 18, 2021 10:58 am

I also tried to debug and I received this error:

error: unbound variable - *TRACK*
if continued: try evaluating symbol again
Function: #<FSubr-LET: #1b182d0>
Arguments:
((LO-RATE (LOWRATE *TRACK* STEP)) (BIGNUM (POWER 2 31)))
(SETF LO-RATE (SUM LO-RATE (- THRESHOLD)))
(SETF *TRACK* (FORCE-SRATE *SOUND-SRATE* LO-RATE))
(MULT BIGNUM (CLIP (S-MAX *TRACK* 0) (/ BIGNUM)))
1>

steve
Site Admin
Posts: 81227
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

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

Post by steve » Thu Feb 18, 2021 11:20 am

ImUsingAudacity wrote:
Thu Feb 18, 2021 10:58 am
I also tried to debug and I received this error:

error: unbound variable - *TRACK*
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:

Code: Select all

;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.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

kozikowski
Forum Staff
Posts: 69221
Joined: Thu Aug 02, 2007 5:57 pm
Operating System: macOS 10.13 High Sierra

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

Post by kozikowski » Thu Feb 18, 2021 11:30 am

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?

Koz

ImUsingAudacity
Posts: 20
Joined: Sun Nov 20, 2016 7:09 pm
Operating System: Windows 10

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

Post by ImUsingAudacity » Thu Feb 18, 2021 2:01 pm

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.

ImUsingAudacity
Posts: 20
Joined: Sun Nov 20, 2016 7:09 pm
Operating System: Windows 10

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

Post by ImUsingAudacity » Thu Feb 18, 2021 2:10 pm

kozikowski wrote:
Thu Feb 18, 2021 11:30 am
I would like to generate a square wave
square waves are made up of a lot of sine waves ....
So why do we need square waves? What's the goal?
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 :)


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,

Trebor
Posts: 9875
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows 8 or 8.1

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

Post by Trebor » Thu Feb 18, 2021 3:21 pm

ImUsingAudacity wrote:
Thu Feb 18, 2021 2:10 pm
... If you have better solution, let me know please ...
FaceRig

ImUsingAudacity
Posts: 20
Joined: Sun Nov 20, 2016 7:09 pm
Operating System: Windows 10

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

Post by ImUsingAudacity » Thu Feb 18, 2021 4:45 pm

Trebor wrote:
Thu Feb 18, 2021 3:21 pm


FaceRig
Thank you, I'll compare both solutions. God bless you,,

Post Reply