Anyone interested in Pitch Detection?

Algorithm development and general DSP issues

Moderator: frank

Post Reply
daeg
Posts: 11
Joined: Mon Apr 06, 2020 5:55 pm

Anyone interested in Pitch Detection?

Post by daeg »

After getting some help with compiling in another thread, I've been playing around with DonStavely's Pitch Detection code from the Infinite Hold"thread. The modeling of the Boss analog method is pretty interesting but the VCO part definitely needs some work. It doesn't spit your fundamental back out!

It got me wondering how it's storing the period, and whether or not the period-> frequency conversion is actually working (based on this thread). Is anyone out there interested in getting Pitch Detection working on the FV-1? So far I've only seen this one attempt.
potul
Posts: 76
Joined: Tue Sep 26, 2017 12:33 am

Re: Anyone interested in Pitch Detection?

Post by potul »

I'm always interested in new effects coming out of the FV-1.

I haven't tried the code, but the key for the period to freq conversion is in two places. First, the perid is determined by a ramp that grows starting from 0 at a positive peak, and stops when you reach the next positive peak. This ramp is nothing more than a "counter" that grows at a rate defined by:

Code: Select all

;
; Update ramp to finish 
;
sof	0, 0.001		; get a small value
rdax	ramp, 1		; add it to the ramp
wrax	ramp, 0		; save it
The higher the offset (0.001 in this case), the faster it will grow.

This ramp value at the end of the peak period get copied into the period, and then some averaging is done. So you end up with a number in the period that represents the time between peaks, but I'm not sure if there is any specific scaling, this depends on the 0.001.

Laster on you need to convert this into a frequency, so you need to do 1/x, using the exp and log pair.

Code: Select all

;
; Need to convert period to frequency
;
rdax	avper, 1		; get smoothed period
log	-1, -0.49	; need 1/X, the offset scales frequency
exp	1, 0		; linear frequency
wrax 	freq, 0		; save it
Then this frequency is used in an oscillator.

The key here is you need to play with the 0.001 and the -0.49 to have the right frequency scaling, if not there yet.
daeg
Posts: 11
Joined: Mon Apr 06, 2020 5:55 pm

Re: Anyone interested in Pitch Detection?

Post by daeg »

Awesome break down. Those are specifically the two parts I'm having the most trouble understanding. The values of 0.001 and 0.49 definitely do not scale musically. Additionally, the higher up on the instrument you go, the more unmusical (non diatonic) it becomes.

Would it be possible to set those offsets / constants to pot values so I could attempt to tune by ear? Since people much smarter than me haven't figured out the equations for those magic values, I'd like to try to hunt for them using the pots.
potul
Posts: 76
Joined: Tue Sep 26, 2017 12:33 am

Re: Anyone interested in Pitch Detection?

Post by potul »

I would start by testing the frequency coefficient and see if you can find the formula for it.

If I'm not mistaken, for this type of oscillator, the coeff is:

k=2*sin(pi* F / Fs)

where:
k=coefficient to use in the oscillator code (freq)
F=target frequency
Fs=Sampling freq

Which is not easy to compute in spinasm. But, as we only are targetting for the fundamental frequencies, this means we will be maximum around 1000 Hz, so the value of sin(pi*F/Fs) can be aproximated to pi*F/Fs

So, we can use

k=2*pi*F/Fs


then I would try to understand the period calculation. this increment of 0.001, how does it translate a period in ms to the final value?

If we are increasing 0.001 in each sampling step, and each sampling step takes 1/Fs seconds, then

period (ms) = avper / (0.001 * Fs)

So for a signal of 80Hz, avper will be 0.409

Now you need to figure out the LOG part and you are set to go...
potul
Posts: 76
Joined: Tue Sep 26, 2017 12:33 am

Re: Anyone interested in Pitch Detection?

Post by potul »

Ok, I did some calculations and this is my result:

you need to make k (the osc coeff):

k = 0.001*2*pi / avper

so, in the log instruction it would be:

Code: Select all

;
; Need to convert period to frequency
;
rdax	avper, 1		; get smoothed period
log	-1, -0.45714	; need 1/X, the offset scales frequency
exp	1, 0		        ; linear frequency
wrax 	freq, 0	; save it
It's not far from what was in the code. Give it a try and let me know how it goes.

If you wonder where the -0.45714 comes from, it's

logbase2(0.001*2*pi)/16
daeg
Posts: 11
Joined: Mon Apr 06, 2020 5:55 pm

Re: Anyone interested in Pitch Detection?

Post by daeg »

Nice :!:

It seems to work. The period value seems to scale. 0.002 gives an octave beneath and 0.0005 gives an octave above.

I'll report back in a few days when I've thoroughly tested it. Much appreciation. :mrgreen:
potul
Posts: 76
Joined: Tue Sep 26, 2017 12:33 am

Re: Anyone interested in Pitch Detection?

Post by potul »

You may find some pitch deviation at the higher frets, caused by the aproximation of the sin we used. But maybe it's not noticeable.
In case it is, some correction could be applied maybe.
potul
Posts: 76
Joined: Tue Sep 26, 2017 12:33 am

Re: Anyone interested in Pitch Detection?

Post by potul »

daeg wrote: Tue May 05, 2020 1:55 pm Nice :!:

It seems to work. The period value seems to scale. 0.002 gives an octave beneath and 0.0005 gives an octave above.

I'll report back in a few days when I've thoroughly tested it. Much appreciation. :mrgreen:
This makes perfect sense as you are in fact dividing and multiplying by 2 the resulting period. So up/down one octave.
potul
Posts: 76
Joined: Tue Sep 26, 2017 12:33 am

Re: Anyone interested in Pitch Detection?

Post by potul »

would you mind posting the resulting code? Or is it just the original one with the changed Log instruction coeff?
igorp
Posts: 65
Joined: Tue May 19, 2015 6:10 am
Location: RU

Re: Anyone interested in Pitch Detection?

Post by igorp »

I did couple of mono synths several years ago.

Main idea was to calculate input frequency and translate it to VCO "input voltage" , like modular synthesis.
So , several "compressors"/normalizers were made and Shmidt triggers too. With good basses it has good result.
I've test it with 8bit (zx-spectrum like) sounds and alittle FM.
https://youtu.be/-DzYsCkQA6A

As you can hear on instagram storie , it have problems on attack and scratches, so it need relative attack detector, etc. But it's funny.


As for infinite sound/hold/freeze , i've tested several algorithms, including crossfade end of loop to beginning, loop feedback, etc.
But they all sound bad. For clean sound it need perfect compression, zerocross detection, loop crossfade, etc. It's possible, imo, but need lot of time to build.
You can try two buffers , one for audio loop, and second - for loop envelope volume , to make fast normalize "on the fly"
Post Reply