Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
plot(n, f(n))
#1
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 !
Reply
#2
In your working example with plot(n, f(n)) the function f() receives a whole array of integers, not a single integer.
Reply
#3
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 ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to plot intraday data of several days in one plot mistermister 3 2,903 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,918 Jun-12-2019, 03:31 PM
Last Post: SriMekala

Forum Jump:

User Panel Messages

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