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

  • SEARCH
  • Home
  • 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 8762699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:35:50+00:00 2026-06-13T15:35:50+00:00

I am trying to implement an FFT based subpixel shifting (translation) algorithm in Python

  • 0

I am trying to implement an FFT based subpixel shifting (translation) algorithm in Python. The Fourier shift theorem allows an array to be translated by a subpixel amount by:
1. Forward FFT array
2. Multiply array by linear phase ramp in Fourier space
3. Inverse FFT array

This algorithm is easy to implement in python using numpy/scipy but its incredibly slow (~10msec) per shift for 256**2 array. I am trying to speed this up by calling c code directly from python using scipy.weave.inline.

I’m having trouble however in passing complex numpy arrays to FFTW. The c code looks like:

    #include <fftw3.h>
    #include <stdlib.h>

    #define INVERSE +1
    #define FORWARD -1


    fftw_complex *i, *o;
    int n, m;
    fftw_plan pf, pi;
    #line 22 "test_scipy_weave.py" 

    i = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * xdim*ydim);
    o = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * xdim*ydim);

    pf = fftw_plan_dft_2d(xdim, ydim, i, o, -1, FFTW_PATIENT);
    pi = fftw_plan_dft_2d(xdim, ydim, o, i,  1, FFTW_PATIENT);

    # Copy data to fftw_complex array. How to use python arrays directly
    for (n=0; n<xdim;n++){
        for (m=0; m<ydim; m++){
            i[n*xdim+m][0]=a[n*xdim+m].real();
            i[n*xdim+m][1]=a[n*xdim+m].imag();
        }
    }

    fftw_execute(pf);

    /* Mult by linear phase ramp here */

    fftw_execute(pi);

    for (n=0; n<xdim;n++){
        for (m=0; m<ydim; m++){
            b[n*xdim+m] = std::complex<double>([in*xdim+m][0], i[n*xdim+m][1]);
        }
    }

    fftw_destroy_plan(p);

So you can see I have to copy the data stored in the numpy array “a” into the fftw_complex array “i”. And again at the end I have to copy the result “i” into the output numpy array “b”. It would be much more efficient to use the numpy arrays “a” and “b” directly in the fftw but I cannot get this to work.

Does anyone have an idea on how to get fftw to use complex numpy arrays directly in scipy.weave.inline?

Thanks

  • 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-06-13T15:35:52+00:00Added an answer on June 13, 2026 at 3:35 pm

    According to the fftw manual, you can import complex.h before fftw.h, which will guarantee that fftw_complex will correspond to the native C data type. I’m pretty sure that numpy data types are also guaranteed to be (or in practice are likely to be) compatible with native C data types.

    In this case you can access a pointer to the array data as a.data_as(ctypes.c_void_p). Unfortunately ctypes doesn’t recognise complex types, but hopefully casting to a void pointer will do the trick.

    When doing this, you have to be careful that your array a is stored in C-contiguous fashion, specified by the parameter order='C' when creating the array.

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

Sidebar

Related Questions

I am trying to implement a 2D FFT using 1D FFTs. I have a
Trying to implement a search similar to here .This searches properties based on city,locality,property
Trying to implement an autocomplete based on this It looks like it's very straight
I have this weird kind of error. I am trying implement basic Euclidean algorithm
I am trying to implement a vision algorithm, which includes a prefiltering stage with
Trying to implement the signature verification of inapp billing on my server (Django/Python). I
I am trying to implement a channel vocoder using the iOS Accelerate vDSP FFT
I'm messing around with some audio stuff and the algorithm I'm trying to implement
I am trying to re-implement one of the matlab toolboxes. they use fft over
I'm trying implement A* Start path finding in my games(which are written with JavaScript,

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.