# Extremum of Cross-Correlation

11 messages
Open this post in threaded view
|

## Extremum of Cross-Correlation

 Is there a way to get the extremum of the cross-correlation of two signals in real-time? Dan had a solution in an old thread in 2009, but claimed it did not work (see below). Then there was another approach in an attachment but the list no longer has the attachment. Any help will be much appreciated. Thanks, Mike ~maxcorr = { |sig1, sig2|   var ch1, ch2, buf1, buf2;   buf1 = LocalBuf(2048);   buf2 = LocalBuf(2048);   ch1 = FFT(buf1, sig1, wintype: 1);   ch2 = FFT(buf2, sig2, wintype: 1);   ch2 = PV_Conj(ch2);   ch1 = PV_Mul(ch1, ch2);   IFFT(ch1); // ignore the result, but hope it comes before BufMax in the synth graph!   BufMax.kr(buf1, ch1 >= 0); } _______________________________________________ sc-users mailing list info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtmlarchive: http://www.listarc.bham.ac.uk/marchives/sc-users/search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

 If you are happy with plain old time-domain correlation (i.e. the "maximal" part is optional, you can avoid the usual spectral domain frustrations by using plain old multiplication. Here's an example: SynthDef.new(\corr, {|bus1, bus2, resultbus, rate=40|         var sig1amp, sig2amp, sig1=In.ar(bus1), sig2=In.ar(bus2);         sig1amp = A2K.kr(                 OpLPF.ar(                         sig1.squared,                         rate,                         iter: 4                 )         );         sig2amp = A2K.kr(                 OpLPF.ar(                         sig2.squared,                         rate,                         iter: 4                 )         );         Out.kr(resultbus,                 A2K.kr(                         OpLPF.ar(                                 sig1*sig2,                                 rate,                                 iter: 4                         )         )/((sig1amp*sig2amp).sqrt.max(0.00001))); }).add; ~outsame = Bus.control; ~outdiff = Bus.control; ~twoHundred={Out.ar(0,SinOsc.ar(200, mul: 0.2))}.play; ~threeHundred={Out.ar(1,SinOsc.ar(300, mul: 0.2))}.play; ~corrsame = Synth.new(\corr, [\bus1, 0, \bus2, 0, \resultbus, ~outsame], addAction:\addToTail); ~corrdiff = Synth.new(\corr, [\bus1, 0, \bus2, 1, \resultbus, ~outdiff], addAction:\addToTail); //results available here: ~outsame.get; ~outdiff.get; NB, that uses a Pseudo-Ugen that I cribbed from the mailing list because sometime you want non-resonant filters with cutoff frequencies. See below. -Dan (Not the dan you referred to in your last mail) ------ // http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/Calculating-OnePole-coefficient-for-one-pole-LP-HP-filter-td4034000.html//see also http://www.dspguide.com///One pole LPF with freq input (instead of coef) //Thanks to Volker B�hm for the (freq -> coef) conversion formula. //And Batuhan Bozkurt for initial pseudo ugen //takes an "iter" param telling you how many times to apply the filter OpLPF {         *kr {|in, freq=100, iter=1|                 var coef = exp(-2*pi * (freq * ControlDur.ir));                 iter.do({in=OnePole.kr(in, coef);});                 ^in;         }         *ar {|in, freq=100, iter=1|                 var coef = exp(-2*pi * (freq * SampleDur.ir));                 iter.do({in=OnePole.ar(in, coef);});                 ^in;         } }
Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

 Wow, Julius, I hadn't noticed the Pitch ugen used autocorrelation. That's neat. Mike, if you wanted to do an n-sample lagged correlation with my version, you would insert an n-sample delay in one of the inputs. If you were going to do many different possible delays it could become computationally inefficient, but how many such delays you wanted would depend on your goal. But with regard to implementation difficulty... not much can be easier than inserting a single Ugen. If modesty prevents Julius from recommending it himself, may I recommend his excellent online book https://ccrma.stanford.edu/~jos/st/Cross_Correlation.htmlfor getting the terminology right here, with regard to "cross correlation", "cross spectrum" (which I think is what you are actually after?) etc. It might clarify the goals a little... -Dan On 02/16/2015 02:02 PM, Julius Smith wrote: > The Pitch Ugen (in DelayUGens.cpp I believe) has a nice implementation of real-time peak-tracking in an AUTO-correlation function.  It would be straightforward to adapt that to a real-time cross-correlation tracker (for continuous inter-channel time-delay estimation etc.). ... > Thanks Dan, > > I have not had a chance to run this, but by looking at it. This is a > correlation of two lists, but does not do the cross-correlation, where > the correlation is taken at a set of time-lags. I think doing that and > then finding the maximum of the time-lagged correlations is the part > that is difficult to do in real-time. Please correct me if I am missing > something. > > -Mike >
Open this post in threaded view
|

## Re: Extremum of Cross-Correlation

 Thanks Dan, I am aware of Julius's online book and indeed reference it all the time! It is an invaluable resource. At this point, I am not sure what is better for my purposes: cross-correlation in the time domain or in the frequency domain (what Julius refers to as "cross spectrum"). The other Dan's version is the latter and can be done rather efficiently (if I did get it working correctly - see my last post). So I am inclined to do that. As you point out, a cross-correlation in the time domain with small n-sample lags might be inefficient. And then there is computing the extremum, which is what I think I was able to tweak in the other Dan's version. More soon Best, Mike On 02/17/2015 04:19 AM, parking-sun wrote: > Wow, Julius, I hadn't noticed the Pitch ugen used autocorrelation. That's > neat. > > Mike, if you wanted to do an n-sample lagged correlation with my version, > you would insert an n-sample delay in one of the inputs. If you were going > to do /many/ different possible delays it could become computationally > inefficient, but how many such delays you wanted would depend on your goal. > But with regard to implementation difficulty... not much can be easier than > inserting a single Ugen. > > If modesty prevents Julius from recommending it himself, may /I/ recommend > his excellent online book > https://ccrma.stanford.edu/~jos/st/Cross_Correlation.html> for getting the terminology right here, with regard to "cross correlation", > "cross spectrum" (which I think is what you are actually after?) etc. It > might clarify the goals a little... > > -Dan > > > On 02/16/2015 02:02 PM, Julius Smith wrote: >> The Pitch Ugen (in DelayUGens.cpp I believe) has a nice implementation of >> real-time > peak-tracking in an AUTO-correlation function.  It would be > straightforward to adapt that to a real-time cross-correlation tracker > (for continuous inter-channel time-delay estimation etc.). > ... >> Thanks Dan, >> >> I have not had a chance to run this, but by looking at it. This is a >> correlation of two lists, but does not do the cross-correlation, where >> the correlation is taken at a set of time-lags. I think doing that and >> then finding the maximum of the time-lagged correlations is the part >> that is difficult to do in real-time. Please correct me if I am missing >> something. >> >> -Mike >> > > > -- > View this message in context: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/Extremum-of-Cross-Correlation-tp7616151p7616205.html> Sent from the SuperCollider Users New (Use this!!!!) mailing list archive at Nabble.com. > > _______________________________________________ > sc-users mailing list > > info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/_______________________________________________ sc-users mailing list info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtmlarchive: http://www.listarc.bham.ac.uk/marchives/sc-users/search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Open this post in threaded view
|