python attributeerror: 'module' object has no attribute 'fft'

Question:

I am a beginner in learning python. Recently in the process of reading one source code on GitHub, I feel confused and have some questions.

First, I feel confused about the code lb.audio.fft.fft.
ok_fft = np.abs(lb.audio.fft.fft(ok_frame))[0:int(len(ok_frame)/2+1)]
This is part of the code about fft but I have looked over the librosa’s offical website(librosa.org) and switch version 0.63/0.7/0.8 to find information about librosa.audio finally I end up with finding nothing.
So, I just want to know if anyone knows about this and tells me about how this part functions?

Second, I will receive an error which shows "attributeerror: ‘module’ object has no attribute ‘fft’" when I try to execute .py file.I have tried to google and solve by myself (I actually find some relevant questions and answers on this site but none of them can fix my error)

This is my first post here, please forgive for any mistakes and format errors. I would appreciate it very much if you can spare your precious time to reply me! Thanks in advance!

code as follows:

import librosa as lb
import numpy as np
import scipy as sc
def scramble_fft(ok_frame):
ok_fft = np.abs(lb.audio.fft.fft(ok_frame))[0:int(len(ok_frame)/2+1)]

new_ok = np.zeros_like(lb.audio.fft.fft(ok_frame))[0:int(len(ok_frame)/2+1)]

i = 0
while(True):
#     print("index ",i)
    if(i>=ok_fft.shape[0]):
        break
    ok_fft_val = ok_fft[i]
    new_ok_val = findExample(ok_fft_val)
    if(np.equal(np.abs(new_ok_val),ok_fft[i])):
        new_ok[i] = new_ok_val
        i += 1
    else:
        continue

new_ok_long = half_to_full(new_ok)
new_ok_long = sc.ifft(new_ok_long)
new_ok_long = throw_imaginary(new_ok_long)
return new_ok_long

def throw_imaginary(fft_arr):
    result = np.zeros(len(fft_arr))
    for i in range(fft_arr.shape[0]):
        result[i] = fft_arr[i].real
    return result


def half_to_full(new):
    new_1 = new
    new_2 = np.delete(new_1, len(new_1)-1)
    new_2 = np.delete(new_2, 0)

    new_flp = np.append(new_1,np.flip(new_2,0))

    for i in range(1,int(len(new_flp)/2)):

        index_read = int(len(new_flp)/2-i)
        index_write = int(len(new_flp)/2+i)

        real = new_flp[index_read].real
        imag = new_flp[index_read].imag
        new_flp[index_write] = real-imag*1j

    return new_flp

Edit:This issue has already been resolved!

Asked By: Ellinia

||

Answers:

I believe at some point lb.audio.fft uses np.fft or something similar and the code you found uses that for some reason.

And as you’ve already known, change lb.audio.fft.fft to np.fft.fft solve the problem.

In Tensorflow 2.0 you can use:

import tensorlfow as tf

tf.signal.fft() or tf.signal.ifft2d

Also tf.signal.fft2d is supported

Answered By: Mohamed Berrimi
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.