Previous topic

pycrtools.core.hftools.mCalibration

Next topic

pycrtools.core.hftools.mFFTW

This Page

pycrtools.core.hftools.mFFT

pycrtools.core.hftools.mFFT.hFFTw()

hFFTw(data_out, data_in)

Apply a forward FFT on a complex vector and return it in a second

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Complex Input vector to make the FFT from.

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0]. If data_in != data_out, the transform is out-of-place and the input array in is not modified. Otherwise, the input array is overwritten with the transform.

Users should note that FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by N.

Usage

outvec.fftw(invec) -> return FFT of invec in outvec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

hFFTw(data_out, data_in)

Apply a real-to-complex FFT using fftw3

Parameters

data_out Return vector in which the FFT (DFT) transformed data is stored. The length is N/2+1.
data_in Real input vector of N elements to make the FFT from.

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

Users should note that FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by N.

The size N can be any positive integer, but sizes that are products of small factors are transformed most efficiently (although prime sizes still use an \mathcal{O}(N \log N) algorithm). The two arguments are input and output arrays of the transform. These vectors can be equal, indicating an in-place transform.

Usage

complexvec.fftw(floatvec) -> return FFT of floatvec in complexvec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

pycrtools.core.hftools.mFFT.hInvFFTw()

hInvFFTw(data_out, data_in)

Apply a backward FFT on a complex vector and return it in a second (scrambles input vector)

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Complex Input vector to make the FFT from.

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

Note that the input vector may be modified and scrambled for a complex-to-complex fft (check!)

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0]. If data_in != data_out, the transform is out-of-place and the input array in is not modified. Otherwise, the input array is overwritten with the transform.

Users should note that FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by N.

Usage

outvec.fftw(invec) -> return backward FFT of invec in outvec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

hInvFFTw(data_out, data_in)

Apply a complex-to-real (inverse) FFT using fftw3

Parameters

data_out Return vector of N real elements in which the inverse FFT (DFT) transformed data is stored.
data_in Complex input vector of The length is N/2+1 elements to make the FFT from.

Usage

vec.invfftw(complexvec) -> return (inv) FFT of complexvec in vec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

pycrtools.core.hftools.mFFT.hSaveInvFFTw()

hSaveInvFFTw(data_out, data_in, nyquistZone)

Apply a backward FFT on a complex vector and return it properly scaled and without scrambling the input vector.

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Complex Input vector to make the FFT from.
nyquistZone Nyquist zone

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

This inverse fft will preserve the input (at the cost of performance, compared to the pure hInvFFTw). It will also swap the data back if it was previously swapped by hNyquistSwap (no swap if NyquistZone=0) and multiply with the correct normalisation factor (1/N).

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0].

Usage

outvec.fftw(invec) -> return backward FFT of invec in outvec

See also

hFFTw(), hInvFFTw(), hNyquistSwap(), hShiftFFT()

hSaveInvFFTw(data_out, data_in, nyquistZone)

Apply a complex-to-real (inverse) FFT using fftw3 and return it properly scaled and without scrambling the input vector

Parameters

data_out Return vector of N real elements in which the inverse FFT (DFT) transformed data is stored.
data_in Complex input vector of The length is N/2+1 elements to make the FFT from.
nyquistZone Nyquist zone

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

This inverse fft will preserve the input (at the cost of performance, compared to the pure hInvFFTw). It will also swap the data back if it was previously swapped by hNyquistSwap (no swap if NyquistZone=0) and multiply with the correct normalisation factor 1/N.

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0].

Usage

outvec.fftw(invec) -> return backward FFT of invec in outvec

See also

hFFTw(), hInvFFTw()

pycrtools.core.hftools.mFFT.hNyquistSwap()

hNyquistSwap(vec, nyquistZone)

Reorders the elements in a complex vector that was created by a FFT according to its Nyquistzone, such that frequencies always increase from left to right

Parameters

vec Complex input and output vector.
nyquistZone Nyquist zone.

Description

The order of the elements in the vector will be reversed and replaced with their negative complex conjugate.

The Nyquist zone n is defined as [(n-1)\nu_\textrm{max}, n\nu_\textrm{max}]

e.g. 1: \nu= [0, \nu_\textrm{max}], 2: \nu=[\nu_\textrm{max}, 2\nu_\textrm{max}], etc ...

The operation is only performed for an even Nyquist Zone, e.g. (2,4,6,..), otherwise nothing is done.

Usage

vec.nyquistswap()

pycrtools.core.hftools.mFFT.hSaveInvFFTw()

hSaveInvFFTw(data_out, data_in, nyquistZone)

Apply a backward FFT on a complex vector and return it properly scaled and without scrambling the input vector.

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Complex Input vector to make the FFT from.
nyquistZone Nyquist zone

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

This inverse fft will preserve the input (at the cost of performance, compared to the pure hInvFFTw). It will also swap the data back if it was previously swapped by hNyquistSwap (no swap if NyquistZone=0) and multiply with the correct normalisation factor (1/N).

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0].

Usage

outvec.fftw(invec) -> return backward FFT of invec in outvec

See also

hFFTw(), hInvFFTw(), hNyquistSwap(), hShiftFFT()

hSaveInvFFTw(data_out, data_in, nyquistZone)

Apply a complex-to-real (inverse) FFT using fftw3 and return it properly scaled and without scrambling the input vector

Parameters

data_out Return vector of N real elements in which the inverse FFT (DFT) transformed data is stored.
data_in Complex input vector of The length is N/2+1 elements to make the FFT from.
nyquistZone Nyquist zone

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

This inverse fft will preserve the input (at the cost of performance, compared to the pure hInvFFTw). It will also swap the data back if it was previously swapped by hNyquistSwap (no swap if NyquistZone=0) and multiply with the correct normalisation factor 1/N.

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0].

Usage

outvec.fftw(invec) -> return backward FFT of invec in outvec

See also

hFFTw(), hInvFFTw()

pycrtools.core.hftools.mFFT.hShiftFFT()

hShiftFFT(outvec, invec, shift)

Multiplies the FFT of a data set with weights such that the data shifts by N samples, where N can be fractional.

Parameters

outvec Output vector containing the shifted FFT
invec Input vector with FFT of data (can be the same as output vector)
shift Shift in fractional samples of the original data

Description

The data simply gets multiplied by a complex weight vector of unity amplitude and linearly increasing phase \phi = 2 \pi u_i \delta t, corresponding to constant time delay. Here `` u_i =

The length of the input and output vector is (N/2+1) if N is the length of the original data.

Usage

hShiftDelay(outvec,invec,shift) -> outvec (fft of shifted data)

Example

#Setup data arrays and fill with random numbers
N=64
data=hArray(float,N)
shifted=hArray(float,N)
data.random(-1.,1.)
fft=hArray(complex,N/2+1)
shifted_fft=hArray(complex,N/2+1)

fft.fftw(data); fft /= N # FFT and normalize properly
data.plot()

#Shift by one sample
shifted_fft.shiftfft(fft,1.)
shifted.invfftw(shifted_fft)
shifted.plot(clf=False)

#Shift half a sample
shifted_fft.shiftfft(fft,.5)
shifted.invfftw(shifted_fft)
shifted.plot(clf=False)
pycrtools.core.hftools.mFFT.hFFTw()

hFFTw(data_out, data_in)

Apply a forward FFT on a complex vector and return it in a second

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Complex Input vector to make the FFT from.

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0]. If data_in != data_out, the transform is out-of-place and the input array in is not modified. Otherwise, the input array is overwritten with the transform.

Users should note that FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by N.

Usage

outvec.fftw(invec) -> return FFT of invec in outvec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

hFFTw(data_out, data_in)

Apply a real-to-complex FFT using fftw3

Parameters

data_out Return vector in which the FFT (DFT) transformed data is stored. The length is N/2+1.
data_in Real input vector of N elements to make the FFT from.

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

Users should note that FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by N.

The size N can be any positive integer, but sizes that are products of small factors are transformed most efficiently (although prime sizes still use an \mathcal{O}(N \log N) algorithm). The two arguments are input and output arrays of the transform. These vectors can be equal, indicating an in-place transform.

Usage

complexvec.fftw(floatvec) -> return FFT of floatvec in complexvec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

pycrtools.core.hftools.mFFT.hInvFFTw()

hInvFFTw(data_out, data_in)

Apply a backward FFT on a complex vector and return it in a second (scrambles input vector)

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Complex Input vector to make the FFT from.

Description

This implementation uses fftw3 - for more information see: http://www.fftw.org/fftw3.pdf

Note that the input vector may be modified and scrambled for a complex-to-complex fft (check!)

The DFT results are stored in-order in the array out, with the zero-frequency (DC) component in data_out[0]. If data_in != data_out, the transform is out-of-place and the input array in is not modified. Otherwise, the input array is overwritten with the transform.

Users should note that FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by N.

Usage

outvec.fftw(invec) -> return backward FFT of invec in outvec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

hInvFFTw(data_out, data_in)

Apply a complex-to-real (inverse) FFT using fftw3

Parameters

data_out Return vector of N real elements in which the inverse FFT (DFT) transformed data is stored.
data_in Complex input vector of The length is N/2+1 elements to make the FFT from.

Usage

vec.invfftw(complexvec) -> return (inv) FFT of complexvec in vec

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

pycrtools.core.hftools.mFFT.hFFTCasa()

hFFTCasa(data_out, data_in, nyquistZone)

Apply an FFT on a vector.

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Vector containing the input data of the FFT.
nyquistZone Nyquist zone

Usage

complexvec.fftcasa(floatvec) -> return FFT for floatvec in complexvec.

See also

hFFTw()

pycrtools.core.hftools.mFFT.hInvFFTCasa()

hInvFFTCasa(data_out, data_in, nyquistZone)

Apply an Inverse FFT on a vector.

Parameters

data_out Return vector in which the inverse FFT transformed data is stored.
data_in Vector containing the input data of the FFT.
nyquistZone Nyquist zone

Usage

floatvec.invfftcasa(complexvec) -> return inverse FFT for complexvec in floatvec.

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

pycrtools.core.hftools.mFFT.hFFT()

hFFT(data_out, data_in, nyquistZone)

Apply an FFT on a vector.

Parameters

data_out Return vector in which the FFT transformed data is stored.
data_in Vector containing the input data of the FFT.
nyquistZone Nyquist zone

Usage

complexvec.fft(floatvec) -> return FFT of floatvec in complexvec.

pycrtools.core.hftools.mFFT.hInvFFT()

hInvFFT(data_out, data_in, nyquistZone)

Apply an inverse FFT on a vector.

Parameters

data_out Return vector in which the inverse FFT transformed data is stored.
data_in Vector containing the input data of the FFT.
nyquistZone Nyquist zone

Usage

floatvec.invfft(complexvec) -> return inverse FFT of complexvec in floatvec.

pycrtools.core.hftools.mFFT.hDoubleFFTPhaseMul()

hDoubleFFTPhaseMul(vec, full_size, nblocks, blocklen, offset)

Multiplies a transposed complex FFT matrix with an appropriate phase factor needed to calculate a second FFT with higher spectral resolution.

Parameters

vec Complex input vector containing the Fourier-transformed and transposed data that was read in with a certain block length, stride, and offset.
full_size Full data size of the time series.
nblocks How many blocks were read in.
blocklen The block length for individual data blocks that were read in.
offset How many blocks were skipped before the first block was read in.

See also

hDoubleFFT(), hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

pycrtools.core.hftools.mFFT.hDoubleFFTPhase()

hDoubleFFTPhase(vec, full_size, nblocks, blocklen, offset)

Returns a vector with phases to be multiplied (after exponentiation) with a transposed complex FFT matrix to calculate a second FFT with higher spectral resolution.

Parameters

vec Output vector containing the phases to be applied to a similar matrix with Fourier-transformed and transposed data that was read in with a certain blocklength, stride, and offset.
full_size Full data size of the time series.
nblocks How many blocks were read in.
blocklen The block length for individual data blocks that were read in.
offset How many blocks were skipped before the first block was read in (smaller or equal to stride).

See also

hDoubleFFT()

pycrtools.core.hftools.mFFT.hDoubleFFT()

hDoubleFFT(vecout, vecin, full_size, nblocks, blocklen, offset)

Performs two subsequent FFTs to obtain a spectrum with higher resolution, allowing one to operate only on a fraction of the data in memory.

Parameters

vecout Complex output of spectrum (potentially containing gaps) - same size as input vector and must not be the same vector.
vecin Complex input vector containing the Fourier-transformed and transposed data that was read in with a certain block length, stride, and offset. NOTE: the input vector will be overwritten and is used as a temporary scratch vector!
full_size Full data size of the time series.
nblocks How many blocks were read in.
blocklen The block length for individual data blocks that were read in.
offset How many blocks were skipped before the first block was read in.

Description

This is how the data is to be provided and dealt with:

First, the time series data is read in as n=nblocks of length l=blocklength each, i.e. giving a vector:

full data -> [B1-1,B1-2,..,B1-l, --STRIDE-1 Blocks to be left out--, B2-1,B2-2,..,B2-l, --STRIDE-1 Blocks to be left out--, ...,  Bn-1,Bn-2,..,Bn-l] 

-> [B1-1,B1-2,..,B1-l, B2-1,B2-2,..,B2-l,  ...,  Bn-1,Bn-2,..,Bn-l] 

(B2-3 means: 2nd block, and 3rd sample within the block)

It is possible to leave out m=stride-1 blocks in between, so that only a fraction 1/(stride+1) of data is read in.

The next step is to transpose the data into a matrix, giving (B1-2 reads data block 1, sample number 2):

Mb=  [ 
   [B1-1,B2-1,..,Bn-1], 
   [B1-2,B2-2,..,Bn-2], 
           ... 
   [B1-l,B2-l,..,Bn-l] 
   ] 

Hence this matrix is incomplete, since it is missing OFFSET times a similar matrix on top (e.g., when one was starting to reading not from the first block, but the 2nd, 3rd etc.) and STRIDE-1-OFFSET matrices at the end.

For clarity: the full matrix would look have looked like (for the example of OFFSET=1 and STRIDE=3 above ... even tough stride is better a power of two!):

Mabc=[ 
     [A1-1,A2-1,..,An-1],       I 
     [A1-2,A2-2,..,An-2],       I 
             ...                I  cdataT 
     [A1-l,A2-l,..,An-l],       I 

     [B1-1,B2-1,..,Bn-1], 
     [B1-2,B2-2,..,Bn-2], 
             ... 
     [B1-l,B2-l,..,Bn-l], 

     [C1-1,C2-1,..,Cn-1], 
     [C1-2,C2-2,..,Cn-2], 
             ... 
     [C1-l,C2-l,..,Cn-l] 
     ] 

The next step is now to take the FFT over rows above and mutliply by the phase factor calculated in this function. Finally, the data is transposed a 2nd time.

Note: Here the function hDoubleFFT1() ends (i.e. with the transpose of the blocks above. This is done to allow for rearranging the blocks in case they were written to disk. Afterwards hDoubleFFT2() picks up again.

The matrix then looks like (if the blocks were merged, which is essentially also a transpose...):

Mabc2=[ 
       [A1-1,A1-2,..,A1-l,B1-1,..,B1-l,C1-1,..,C1-l],     I   tmpspec 
       [A2-1,A2-2,..,A2-l,B2-1,..,B2-l,C2-1,..,C2-l],     I 
       ... 
       now take only n/stride rows per memory chunk 
       ... 
       [An-1,An-2,..,An-l,Bn-1,..,Bn-l,Cn-1,..,Cn-l], 
      ] 

where one now needs to split the matrix again (after n/stride rows each) to work with the same memory size.

In a second step (DoubleFFT2()), the FFT is taken a second time (again over what are then the rows above) and the result is tranposed a last time, giving the final spectrum (with gaps, if the blocks are not properly rearranged again):

Mfinal=[ 
        [A1-1,A2-1,..,An-1],      (specT: only first nblock_section columns) 
        [A1-2,A2-2,..,An-2],       specT2: only first blocklen rows and 
          ...                        nblock_section columns 
        [A1-l,A2-l,..,An-l], 
        [B1-1,B2-1,..,Bn-1], 
        [B1-2,B2-2,..,Bn-2], 
                ... 
        [B1-l,B2-l,..,Bn-l]] 
        [C1-1,C2-1,..,Cn-1], 
        [C1-2,C2-2,..,Cn-2], 
                ... 
        [C1-l,C2-l,..,Cn-l] 
       ] 

See also

hFFT(), hInvFFTw(), hSaveInvFFTw(), hNyquistSwap(), hShiftFFT()

Example

>>> # Input parameters
>>> ncolumns = 128
>>> nrows = 1024  # = bigFFTblocksize / ncolumns
>>> bigFFTblocksize = ncolumns*nrows

>>> # Prepare some vectors
>>> a=hArray(complex,[nrows, ncolumns])
>>> a.random(-2.0,2.0)
>>> b=hArray(complex,copy=a)
>>> bT=b.transpose()

>>> # Do only one big FFT
>>> bigFFT=hArray(complex,[bigFFTblocksize])
>>> bigFFT.fftw(a)

>>> # Do two FFTs with the steps described above
>>> aT=hArray(a).transpose()
>>> aT[...].fftw(aT[...])
>>> aT.doublefftphasemul(bigFFTblocksize,nrows,ncolumns,0)
>>> a.transpose(aT)
>>> a[...].fftw(a[...])
>>> aT.transpose(a)

>>> # Do the steps just above in one go
>>> bT.doublefft(b,bigFFTblocksize,nrows,ncolumns,0)
bigFFT, aT, & bT all contain the same spectra
pycrtools.core.hftools.mFFT.hDoubleFFT1()

hDoubleFFT1(vecout, vecin, full_size, nblocks, blocklen, offset)

Performs two subsequent FFTs to obtain a spectrum with higher resolution, allowing one to operate only on a fraction of the data in memory.

Parameters

vecout Complex output of spectrum (potentially containing gaps) - same size as input vector and must not be the same vector.
vecin Complex input vector containing the Fourier-transformed and transposed data that was read in with a certain block length, stride, and offset. NOTE: the input vector will be overwritten and is used as a temporary scratch vector!
full_size Full data size of the time series.
nblocks How many blocks were read in.
blocklen The block length for individual data blocks that were read in.
offset How many blocks were skipped before the first block was read in.

Description

NOTE: here is output is in the input vector vecin - vecout is just a scratch vector

See also

hDoubleFFT()

pycrtools.core.hftools.mFFT.hDoubleFFT2()

hDoubleFFT2(vecout, vecin, nblocks, blocklen)

Performs two subsequent FFTs to obtain a spectrum with higher resolution, allowing one to operate only on a fraction of the data in memory.

Parameters

vecout Complex output of spectrum - same size as input vector and must not be the same vector.
vecin Complex input vector obtained from DoubleFFT1
nblocks How many blocks were read in.
blocklen The block length for individual data blocks that were read in.

See also

hDoubleFFT()

Example

>>> cdataT[offset].doublefft(cdata[offset],fullsize,nblocks,blocklen,offset)

>>> # This is the same as:

>>> cdataT[offset].doublefft1(cdata[offset],fullsize,nblocks,blocklen,offset)
>>> cdataT[offset].doublefft2(cdata[offset],nblocks,blocklen)

>>> # but result is in cdata (and not cdataT)
pycrtools.core.hftools.mFFT.hFFTFrequencies()

hFFTFrequencies(frequencies, sampleFrequency, nyquistZone)

Get frequency values corresponding to FFT depending on the blocksize (derived from the length of the output array), sample frequency and Nyquist zone.

Parameters

frequencies Output vector with frequency values.
sampleFrequency Sample frequency.
nyquistZone Nyquist zone.