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 3600466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:29:55+00:00 2026-05-18T20:29:55+00:00

I have a two dimensional array, i.e. an array of sequences which are also

  • 0

I have a two dimensional array, i.e. an array of sequences which are also arrays. For each sequence I would like to calculate the autocorrelation, so that for a (5,4) array, I would get 5 results, or an array of dimension (5,7).

I know I could just loop over the first dimension, but that’s slow and my last resort. Is there another way?

Thanks!

EDIT:

Based on the chosen answer plus the comment from mtrw, I have the following function:

def xcorr(x):
  """FFT based autocorrelation function, which is faster than numpy.correlate"""
  # x is supposed to be an array of sequences, of shape (totalelements, length)
  fftx = fft(x, n=(length*2-1), axis=1)
  ret = ifft(fftx * np.conjugate(fftx), axis=1)
  ret = fftshift(ret, axes=1)
  return ret

Note that length is a global variable in my code, so be sure to declare it. I also didn’t restrict the result to real numbers, since I need to take into account complex numbers as well.

  • 1 1 Answer
  • 0 Views
  • 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-18T20:29:55+00:00Added an answer on May 18, 2026 at 8:29 pm

    Using FFT-based autocorrelation:

    import numpy
    from numpy.fft import fft, ifft
    
    data = numpy.arange(5*4).reshape(5, 4)
    print data
    ##[[ 0  1  2  3]
    ## [ 4  5  6  7]
    ## [ 8  9 10 11]
    ## [12 13 14 15]
    ## [16 17 18 19]]
    dataFT = fft(data, axis=1)
    dataAC = ifft(dataFT * numpy.conjugate(dataFT), axis=1).real
    print dataAC
    ##[[   14.     8.     6.     8.]
    ## [  126.   120.   118.   120.]
    ## [  366.   360.   358.   360.]
    ## [  734.   728.   726.   728.]
    ## [ 1230.  1224.  1222.  1224.]]
    

    I’m a little confused by your statement about the answer having dimension (5, 7), so maybe there’s something important I’m not understanding.

    EDIT: At the suggestion of mtrw, a padded version that doesn’t wrap around:

    import numpy
    from numpy.fft import fft, ifft
    
    data = numpy.arange(5*4).reshape(5, 4)
    padding = numpy.zeros((5, 3))
    dataPadded = numpy.concatenate((data, padding), axis=1)
    print dataPadded
    ##[[  0.   1.   2.   3.   0.   0.   0.   0.]
    ## [  4.   5.   6.   7.   0.   0.   0.   0.]
    ## [  8.   9.  10.  11.   0.   0.   0.   0.]
    ## [ 12.  13.  14.  15.   0.   0.   0.   0.]
    ## [ 16.  17.  18.  19.   0.   0.   0.   0.]]
    dataFT = fft(dataPadded, axis=1)
    dataAC = ifft(dataFT * numpy.conjugate(dataFT), axis=1).real
    print numpy.round(dataAC, 10)[:, :4]
    ##[[   14.     8.     3.     0.     0.     3.     8.]
    ## [  126.    92.    59.    28.    28.    59.    92.]
    ## [  366.   272.   179.    88.    88.   179.   272.]
    ## [  734.   548.   363.   180.   180.   363.   548.]
    ## [ 1230.   920.   611.   304.   304.   611.   920.]]
    

    There must be a more efficient way to do this, especially because autocorrelation is symmetric and I don’t take advantage of that.

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

Sidebar

Related Questions

I have a two dimensional array, i.e. an array of sequences which are also
I have a two dimensional array that I need to load data into. I
I have a two-dimensional array (of Strings) which make up my data table (of
wonder if anyone can help; I have an a two dimensional array which I
I have a header file for a game that declares a two-dimensional array for
Inspired by Raymond Chen's post , say you have a 4x4 two dimensional array,
I have two applications written in Java that communicate with each other using XML
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have a two-dimensional array of ints, for example: int [][] board= { {23,17,3,29,12,10},

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.