Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some errors
#1
Hi, I've this code and I think that's correctly write but it show me some errors. I tried deal with this but without effects

import numpy
from numpy.fft import fft
from numpy import sin, cos, pi, ones, zeros, arange, r_, sqrt, mean

def rFFT(x):
    
    n = len(x)
    if(n == 1):
        return x
    
    
    w = getTwiddle(n)
    m = n/2;
    X = ones(m, float)*1j
    Y = ones(m, float)*1j
    
    for k in range(m):
        X[k] = x[2*k]
        Y[k] = x[2*k + 1] 

    X = rFFT(X)  
    Y = rFFT(Y) 

    F = ones(n, float)*1j
    for k in range(n):
        i = (k%m)
        F[k] = X[i] + w[k] * Y[i]

    return F

def getTwiddle(NFFT=8):

    W = r_[[1.0 + 1.0j]*NFFT]

    for k in range(NFFT):
        W[k] = cos(2.0*pi*k/NFFT) - 1.0j*sin(2.0*pi*k/NFFT)

    return W

def DFT(x, N=8):
    y = [1.0 + 1.0j]*N
    y = r_[y]
    for n in range(N):
        wsum = 0 + 0j;
        for k in range(N):
            wsum = wsum + (cos(2*pi*k*n/N) - (1.0j * sin(2*pi*k*n/N)))*x[k]

        y[n] = wsum
        
        
    return y

def test_rfft(N      = 64,      
              nStart = 0.2,     
              nStep  = 2.1,     
              pStep  = pi/4,    
              limErr = 10e-12,
              maxErr = 0    
              ):
     for s in arange(nStart, N+nStep, nStep):
        for p in arange(0, pi+pStep, pStep):

            n = arange(N, 0, -1)
            x = cos(2*pi*n/s + p)

            xDFT = DFT(x,N)
            nFFT = fft(x,N)
            xFFT = rFFT(x)

            rmsErrD = sqrt(mean(abs(xDFT - xFFT))**2)
            rmsErrN = sqrt(mean(abs(nFFT - xFFT))**2)

            if rmsErrD > limErr or rmsErrN > limErr:
                print (s, p, "Error!", rmsErrD, rmsErrN)
                print (xDFT)
                print (nFFT)
                print (xFFT)

            if rmsErrD > maxErr:
                maxErr = rmsErrD
            elif rmsErrN > maxErr:
                maxErr = rmsErrN

            print("N %d maxErr = %f " % (N,maxErr))
    

if __name__ == '__main__':
    tv = 2**arange(1,12)
    for nfft in tv:
        test_rfft(N=nfft)
Errors:
Error:
Traceback (most recent call last): File "/home/boyan/sandbox2/forum.py", line 90, in <module> test_rfft(N=nfft) File "/home/boyan/sandbox2/forum.py", line 68, in test_rfft xFFT = rFFT(x) File "/home/boyan/sandbox2/forum.py", line 14, in rFFT X = ones(m, float)*1j File "/home/boyan/sandbox2/sbox/lib/python3.7/site-packages/numpy/core/numeric.py", line 214, in ones a = empty(shape, dtype, order) TypeError: 'float' object cannot be interpreted as an integer
buran write Jan-06-2021, 04:42 PM:
Please, show unmodified traceback. The whole thing is one error.
Reply
#2
When you do division, the result is always float. so on line#13 you have m = n/2; and m is float, but numpy.ones() expect int or sequence of ints as first argument shape. You need to convert it to int or use floor division.

>>> 4/2
2.0
>>> int(4/2)
2
>>> 4//2
2
>>> 5//2
2
>>> int(5/2)
2
and by the way - don't use ; at the end of the line - no need of it.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks, that works!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020