Python Forum

Full Version: plot(n, f(n))
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm having a hard time understanding something:
from pylab import *
import numpy as np
import numpy.fft as npft
import scipy.io.wavfile as wavfile
import math

fs_u1 , u1 = wavfile.read("u1.wav") #u1 is a numpy array of complex values

n= np.arange(0,u1.size)

def module(z): # returns the module of the complex z
    a = z.real
    b = z.imag
    return math.sqrt(a**2+b**2)

def filtrage(M, y) : # should take an int M and a numpy array y, return a numpy array
    ychap = np.fft.fft(y)
    N = ychap.size
    mask = np.ones(N)
    coord = np.arange(M+1,N-M,1)
    mask[coord]=0
    yfiltchap = mask*ychap
    yfilt = np.fft.ifft(yfiltchap)
    yfilt = yfilt.real
    return yfilt

def divisionEuclide(x): # should return the sum of the modules of all the complex value of the numpy array sent to it
    sum = 0
    for i in(n):
        sum+= module(x[i])**2
    return sum


plot(n, divisionEuclide(u1-filtrage(n,u1)))
returns the error:
Error:
File "...", line 29, in filtrage coord = np.arange(M+1,N-M,1) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


I thought the "n" sent to filtrage() by "plot(n, divisionEuclide(u1-filtrage(n,u1)))" was a integer of the array n, not the array itself, since :
n = np.arange(0,10)

def f(x):
    return 2*x
plot(n, f(n))
works just fine.
So why is the whole array sent to the function filtrage in the first example ? And how do I change that ?
I hope you can help me, thank you !
In your working example with plot(n, f(n)) the function f() receives a whole array of integers, not a single integer.
Thank you for answering !
I misunderstood the way plot() works then. But how would you plot divisionEuclide(u1-filtrage(n,u1)) in function of the values contained in n ?