Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 311611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:56:27+00:00 2026-05-12T07:56:27+00:00

I’m using cython for a correlation calculation in my python program. I have two

  • 0

I’m using cython for a correlation calculation in my python program. I have two audio data sets and I need to know the time difference between them. The second set is cut based on onset times and then slid across the first set. There are two for-loops: one slides the set and the inner loop calculates correlation at that point. This method works very well and it’s accurate enough.

The problem is that with pure python this takes more than one minute. With my cython code, it takes about 17 seconds. This still is too much. Do you have any hints how to speed-up this code:

import numpy as np
cimport numpy as np

cimport cython

FTYPE = np.float
ctypedef np.float_t FTYPE_t

@cython.boundscheck(False)
def delay(np.ndarray[FTYPE_t, ndim=1] f, np.ndarray[FTYPE_t, ndim=1] g):
    cdef int size1 = f.shape[0]
    cdef int size2 = g.shape[0]
    cdef int max_correlation = 0
    cdef int delay = 0
    cdef int current_correlation, i, j

    # Move second data set frame by frame
    for i in range(0, size1 - size2):
        current_correlation = 0

        # Calculate correlation at that point
        for j in range(size2):
            current_correlation += f[<unsigned int>(i+j)] * g[j]

        # Check if current correlation is highest so far
        if current_correlation > max_correlation:
            max_correlation = current_correlation
            delay = i

    return delay
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:56:27+00:00Added an answer on May 12, 2026 at 7:56 am

    Edit:
    There’s now scipy.signal.fftconvolve which would be the preferred approach to doing the FFT based convolution approach that I describe below. I’ll leave the original answer to explain the speed issue, but in practice use scipy.signal.fftconvolve.

    Original answer:
    Using FFTs and the convolution theorem will give you dramatic speed gains by converting the problem from O(n^2) to O(n log n). This is particularly useful for long data sets, like yours, and can give speed gains of 1000s or much more, depending on length. It’s also easy to do: just FFT both signals, multiply, and inverse FFT the product. numpy.correlate doesn’t use the FFT method in the cross-correlation routine and is better used with very small kernels.

    Here’s an example

    from timeit import Timer
    from numpy import *
    
    times = arange(0, 100, .001)
    
    xdata = 1.*sin(2*pi*1.*times) + .5*sin(2*pi*1.1*times + 1.)
    ydata = .5*sin(2*pi*1.1*times)
    
    def xcorr(x, y):
        return correlate(x, y, mode='same')
    
    def fftxcorr(x, y):
        fx, fy = fft.fft(x), fft.fft(y[::-1])
        fxfy = fx*fy
        xy = fft.ifft(fxfy)
        return xy
    
    if __name__ == "__main__":
        N = 10
        t = Timer("xcorr(xdata, ydata)", "from __main__ import xcorr, xdata, ydata")
        print 'xcorr', t.timeit(number=N)/N
        t = Timer("fftxcorr(xdata, ydata)", "from __main__ import fftxcorr, xdata, ydata")
        print 'fftxcorr', t.timeit(number=N)/N
    

    Which gives the running times per cycle (in seconds, for a 10,000 long waveform)

    xcorr 34.3761689901
    fftxcorr 0.0768054962158
    

    It’s clear the fftxcorr method is much faster.

    If you plot out the results, you’ll see that they are very similar near zero time shift. Note, though, as you get further away the xcorr will decrease and the fftxcorr won’t. This is because it’s a bit ambiguous what to do with the parts of the waveform that don’t overlap when the waveforms are shifted. xcorr treats it as zero and the FFT treats the waveforms as periodic, but if it’s an issue it can be fixed by zero padding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 162k
  • Answers 162k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sure you can use a VB COM component in an… May 12, 2026 at 11:58 am
  • Editorial Team
    Editorial Team added an answer Side note: Your belongs_to :payment_types should be belongs_to :payment_type. You… May 12, 2026 at 11:58 am
  • Editorial Team
    Editorial Team added an answer App_Data only exists for ASP.NET web sites and web applications.… May 12, 2026 at 11:58 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.