Notch filter with loop

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
Michael Kazmierski
Posts: 75
Joined: Wed Aug 17, 2016 1:06 am
Operating System: Windows 10

Notch filter with loop

Post by Michael Kazmierski » Thu May 03, 2018 11:39 pm

Hi everyone,
So I'm faced with a rather daunting task here to correct a tonal issue of one of my favorite instruments.
I have severl solo recordings of Italian Zampogna bagpipes, and most of them, regardless of their key, have this absolutely piercing, loud "sopranina" drone that gets totally in the way of anything else, which makes it absolutely impossible to hear anything besides it, even though this isn't intentional. Obviously the frequencies will change depending on the sopranina's note, but there are times when I only know the base frequency to notch and would want to notch out all the other harmonics of that frequency with the same Q value.
I always did it manually by simply calculating the frequency of each and every harmonic of the sopranina, but this is madly time-consuming for a blind person like myself, and there is always the potential error of being wrong when typing in each frequency. So I'd like to see if anyone can come up with this.
Basically it would be a plugin (not just a Nyquist prompt), a Notch filter that would notch out first the base frequency that you type in, then all the other harmonics (it would add your frequency to the number in a loop), and the Q value for all the frequencies would be stored. For example, I have a nice track where a bass Zampogna is being played along with a (not-so-good) singer, and after matching the pitch of the sopranina to a Sawtooth wave, I determined that the sopranina's frequency is 305. I did try this manually, but boy was it painfully tedious!
I think I might know enough about Nyquist to convert the Notch function into the EQ function, but I thought I could start by this Notch plugin, in conjunction with mixing it with the original to even out the sopranina with the other pipes, to make remastering the Zampogna recordings a breeze. I simply could do an inverted delay, but for one, it's not too accurate, 2, you have to divide 1 by the frequency you're trying to counteract (not so easy), and 3, the odd harmonics an octave below are greatly emphasized, not what I want here.
Again, it's simply a Notch filter function just with an added loop counter. It would do this for every subsequent harmonic up to the sample rate. The Hum Remover plugin doesn't accomplish my task so I thought I'd see about this kind here.
I've attached the beginning of that nice solo with a rather overbearing sopranina so you can hear for yourself what I'm refering to. I've deleted the first verse onwards, so it's just a solo and a quick solo at that. I'm absolutely sure you'll hear why I hate that sopranina!

Thanks


Michael
Attachments
Sopranina 6P.mp3
(754.08 KiB) Downloaded 28 times

Trebor
Posts: 4658
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

Re: Notch filter with loop

Post by Trebor » Fri May 04, 2018 4:04 am

Michael Kazmierski wrote:... there are times when I only know the base frequency to notch and would want to notch out all the other harmonics of that frequency...
It's similar to the vuvuzela problem. DeHum plugin does remove most of it if set to 152.5Hz ...
'Sopranina 6P' minus 152-5Hz & harmonics.flac
(1.28 MiB) Downloaded 37 times
The slight vibrato means the notch-filters won't remove it all.

Michael Kazmierski
Posts: 75
Joined: Wed Aug 17, 2016 1:06 am
Operating System: Windows 10

Re: Notch filter with loop

Post by Michael Kazmierski » Fri May 04, 2018 12:57 pm

Nice try, Trebor. However, I noticed that you removed both the tenor AND sopranina drones. My intention was only to delete just the sopranina, just harmonics of 305, and keep the tenor's odd harmonics. I'd mix with the original and adjust the volume of sopranina so the pipes are rich, yet not totally drowned out by the sopranina as originally heard. In the modern world, nobody uses sopranina drones anymore. This was recorded in the 70's, and in fact I did not notice any vibrato myself until you pointed that out! Go figure, I have perfect pitch myself!
Well, I have played around with the DeHum remover and it looks promising, but the problem with that is that I notice the Q gets tighter the higher you go, but in the code there is no indication of that. So I tried to design one that basically literally does notch filters, including Q values, in a loop. I've tried reading the debug output several times and I'm just sunk. Thing is, nobody taught me to program in Nyquist, so do you mind fixing this up for me? I don't want the Q values to change regardless of the frequency. Oh by the way, if possible, perhaps you could add a choice between all harmonics to notch, even harmonics only, or odd harmonics only. Essentially the Dehum, but with the Q values the same across the width, and none of this stupid noiseegate business. Also, I'd suggest keeping it so it counts the harmonics automatically so no "number of harmonics", it would just do it.


;nyquist plug-in
;version 1
;type process
;name "Note notcher.0"
;action "Removing harmonics, ( this may take some time ) ..."
;info: Unintentional reverb effect. Gibbs ringing on transients.
;info: if you find a cure for the reverb please email me: [email protected]

;control freq "Frequency" real "" 60 1 20000
;control q "Width" real " higher is more" 10 0.01 100
(setq mysound s)
(setq q q) ; set the base Q for the filter
(setq r *sound-srate*)
(setq iter (truncate (/ (/ r freq) 2)))
(defun notch (mysound freq q iter))
(dotimes (i iter mysound))
(setf mysound (notch2 mysound (* freq (1+ i)))

(multichan-expand #'notch2 mysound freq q iter))

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

Re: Notch filter with loop

Post by steve » Fri May 04, 2018 2:26 pm

Michael Kazmierski wrote: I'd suggest keeping it so it counts the harmonics automatically so no "number of harmonics"
There has to at least be a count of the notches within the code - you don't want it looping to infinity. So even if you don't have a user control for the number of notches, you have to decide how many notches you want.
Michael Kazmierski wrote:Thing is, nobody taught me to program in Nyquist
None of us have been taught to program in Nyquist either. As far as I'm aware, the only place that Nyquist is taught is at CMU (where Nyquist was invented). However, Nyquist is a relatively simple language, and it is pretty well documented these days (plus support on this forum).
Michael Kazmierski wrote:I notice the Q gets tighter the higher you go
There's a reason for that.

The Q of a notch filter sets the width, which is a proportion of the centre frequency. For example, for Q = 1, the width is about +/- 1 octave of the centre frequency.
For easy calculation, let's say that Q = 1. That means that for a notch with a centre frequency of 100 Hz, the -3dB width of the notch is around 50 Hz to 200 Hz, a range of 150 Hz.
Now if we look at the notch centred at 1000 Hz, the notch is now around 500 Hz to 2000 Hz, a width of 1500 Hz. If the Q is not increased for higher frequency values, we end up taking out nearly all of the high frequency sound because the notches are so wide.

Also, the higher the Q, the more ringing occurs, so for hum removal there is a need to compromise between not removing too much, and not causing too much ringing.
For your application it may be acceptable to have a fixed Q for all notches, but narrower notches at higher frequencies tends to be best for hum removal.
Michael Kazmierski wrote:and none of this stupid noiseegate business
Again, there's a reason for that. It may not be appropriate for your application, but for hum removal it makes sense (not stupid) to minimise damage to the audio by only applying the notches where it is really needed.

Here's some plug-in code for you. Feel free to modify it to suit your needs, and feel free to tell us about any such modifications:

Code: Select all

;nyquist plug-in
;version 4
;type process
;name "Multi-Notch"
;action "Filtering (this may take some time) ..."
;author "Steve Daulton"
;copyright "Released under terms of the GNU General Public License version 2"

;control freq "Base frequency" float "Hz" 100 10 2000
;control hz-max "Maximum filter frequency" float "kHz" 10 1 20
;control mode "Which harmonics" choice "All,Even,Odd" 0
;control q "Filter Q" real " higher narrower" 10 0.01 100

;; notch frequency must be less than (/ *sound-srate* 2.0)
(setf hz-max (min (* 1000 hz-max)(/ *sound-srate* 2.0)))

(defun notch (sig)
  (setf sig (notch2 sig freq q))
  (setq iterations (/ hz-max freq))
  (case mode
    (0  (setf hz (* freq 2))
        (setf step 1)
        (setf multiplier 2))
    (1  (setf hz (* freq 2))
        (setf step 2)
        (setf multiplier 2))
    (t  (setf hz (* freq 3))
        (setf step 2)
        (setf multiplier 3)))
  (do ((hz hz (* freq multiplier)))
      ((>= hz hz-max) sig)
    ;(print hz)
    (setf sig (notch2 sig hz q))
    (setf multiplier (+ multiplier step))))

(multichan-expand #'notch *track*)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Michael Kazmierski
Posts: 75
Joined: Wed Aug 17, 2016 1:06 am
Operating System: Windows 10

Re: Notch filter with loop

Post by Michael Kazmierski » Fri May 04, 2018 7:21 pm

It's not showing up in my Effect menu even after restarting Audacity. I did save it as a plugin, but apparently it doesn't show up for some reason. Maybe because it uses version 4 syntax and I still use 2.0.6.
Hmm - let's see - the only differences I'm aware of are from version 3 and version 4, is that "S" in version 3 is replaced by "*track*" in version 4. I edited that so it would reflect that, plus changed the version to version 3. My guess is that "Sig" isn't recognized or something? Now if only it would show up. Reading the code, however, it looks quite promising.

Michael Kazmierski
Posts: 75
Joined: Wed Aug 17, 2016 1:06 am
Operating System: Windows 10

Re: Notch filter with loop

Post by Michael Kazmierski » Sat May 05, 2018 12:48 am

It's still not showing up! HELP PLEASE!
Attachments
Groupnotch.ny
(1.1 KiB) Downloaded 25 times

Michael Kazmierski
Posts: 75
Joined: Wed Aug 17, 2016 1:06 am
Operating System: Windows 10

Re: Notch filter with loop

Post by Michael Kazmierski » Sat May 05, 2018 1:21 am

Well, my friend helped me figure it out why it wasn't showing up. He looked at the Log, and it says that it couldn't load the plugin because it wasn't Unicode when I copied in to a TXT file and saved it as a Ny.
After trying your original as well as my slightly modified version, there's still a bug and I can't pinpoint what it is.
Here's the Debug output with the default settings.
"Nyquist returned the value: 10000"

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

Re: Notch filter with loop

Post by steve » Sat May 05, 2018 1:45 am

Your final line is wrong.
You have this:

Code: Select all

(multichan-expand #'notch2 s freq q)
but it should be "notch" and not "notch2".
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Michael Kazmierski
Posts: 75
Joined: Wed Aug 17, 2016 1:06 am
Operating System: Windows 10

Re: Notch filter with loop

Post by Michael Kazmierski » Sat May 05, 2018 3:15 am

That still causes a bug! Something must be wrong with Hz-Max I guess? I'll attach the bug.
Attachments
Bugs.txt
(61.37 KiB) Downloaded 22 times

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

Re: Notch filter with loop

Post by steve » Sat May 05, 2018 9:20 am

Why don't you just update Audacity to the current version? You can get it here: https://www.audacityteam.org/download/
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply