discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] Cross Correlation Function


From: Martin Dvh
Subject: Re: [Discuss-gnuradio] Cross Correlation Function
Date: Mon, 27 Mar 2006 01:55:09 +0200
User-agent: Debian Thunderbird 1.0.2 (X11/20051002)

sunflower wrote:
> Hi,
> Did anybody know how to implement cross correlation function? Thanks.
> It is not gr_simple_correlator, is it?
> Thanks
> 
> 
> _______________________________________________
> Discuss-gnuradio mailing list
> address@hidden
> http://lists.gnu.org/mailman/listinfo/discuss-gnuradio
> 
If you want a generic correlation function then you can do correlation in the 
frequency domain quite efficiently.

See the code below on how to do this:

This code is very well suited if you want the whole correlation function over a 
reletively large time-frame.
This code is maybe not the best way when you only want to know where the  
correlation peak is and already know where it approximately should be.

You can also find this code on:
http://www.olifantasia.com/pub/projects/gnuradio/mdvh/passive_radar

(Look for the file named correlator.py)
I extracted it from my passive radar experiments code which you also can find 
there.
(But which are not very readable)


class correlator_c(gr.hier_block):
    def __init__(self, fg,  fft_size=512,output_type='COMPLEX'):
        #This Hier_block expects an input block with two interleaved gr_complex 
signals
        #It outputs fft_size blocks with time zero at the middle of the block
        #Output type can be chosen between 'COMPLEX', 'REAL', 'MAG' or 'ARG'
        #
        #You can use it in the following way:
        # interleaver= gr.interleave(gr.sizeof_gr_complex)
        # fg.connect(src0,(interleaver,0))
        # fg.connect(src1,(interleaver,1))
        # corr=correlator.correlator_c(fg=fg,fft_size=512,output_type='COMPLEX')
        # fg.connect(interleaver,corr)
        #


        di = gr.deinterleave(gr.sizeof_gr_complex)
        s2p_a = gr.serial_to_parallel(gr.sizeof_gr_complex, fft_size)
        s2p_b = gr.serial_to_parallel(gr.sizeof_gr_complex, fft_size)
        s2p3 = gr.serial_to_parallel(gr.sizeof_gr_complex, fft_size)
        p2s_a = gr.parallel_to_serial(gr.sizeof_gr_complex, fft_size)
        p2s_b = gr.parallel_to_serial(gr.sizeof_gr_complex, fft_size)

        mywindow = fftsink.window.blackmanharris(fft_size)

        fft_a = gr.fft_vcc(fft_size, True, mywindow)
        fft_b = gr.fft_vcc(fft_size, True, mywindow)
        ifft=gr.fft_vcc(fft_size, False, mywindow)


        conj=gr.conjugate_cc()
        mult=gr.multiply_cc()


        #get the ffts of the input signals (go from time to frequency domain)
        fg.connect((di,0),s2p_a,fft_a,p2s_a)
        fg.connect((di,1),s2p_b,fft_b,p2s_b)

        #do the correlation in the frequency domain
        fg.connect(p2s_a,conj)
        fg.connect(p2s_b,(mult,0))
        fg.connect(conj,(mult,1))

        #transform back to the time domain
        fg.connect(mult,s2p3,ifft)

        if output_type=='REAL':
          c2real = gr.complex_to_real(fft_size)
        elif output_type=='MAG':
          c2real=gr.complex_to_mag(fft_size)
        elif output_type=='ARG':
          c2real=gr.complex_to_arg(fft_size)
        if output_type=='COMPLEX':
          sink=ifft
        else:
          fg.connect(ifft,c2real)
          sink=c2real

        gr.hier_block.__init__(self, fg, di, sink)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]