Python Forum

Full Version: how can I create graphics using matplotlib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, I'm trying to use the matplotlib library and since I'm new my code has been written wrong, can you tell me what I failed ?, more than anything I would like to be corrected
x = np.arange(1,41)
b = 0.7
a =(1 - b) / (1-b**5)
i = np.arange(1,5)

w = a*(b**(5-i))
for n in range[5:40]:
    y = [w*x[n-4:n].conj().T]
n = range[5:40]
plt.plot(x,y)
plt.show()
my question from the book is the following:

  1. Generate a MATLAB graph of the output response and y[n], for 5 <= n <= 40 of the filter
    PMPE in point 5, with b = 0.7, when the input x[n] is:

    a) x[n] = 1, 0 <= n <=40
graphic that I want to obtain
[Image: fu4du3k5scs6je27g.jpg]
I would appreciate any help from you
There seem to be many errors in this script, both python errors and mathematical errors. Can you describe the mathematical relation between x and y?
(Nov-20-2018, 01:20 PM)Gribouillis Wrote: [ -> ]There seem to be many errors in this script, both python errors and mathematical errors. Can you describe the mathematical relation between x and y?
ok,sorry for the delay
[Image: dy6u5128enq07ay7g.jpg]
It is still unclear, what are the possible values of n? When i varies from 0 to N-1, the index n-i decreases from n to n-N+1, which may be negative. Can you explain this point?

It looks like a convolution, some special functions such as numpy.convolve may be useful later once we understand what you are doing.
(Nov-20-2018, 05:16 PM)Gribouillis Wrote: [ -> ]It is still unclear, what are the possible values of n? When i varies from 0 to N-1, the index n-i decreases from n to n-N+1, which may be negative. Can you explain this point?

It looks like a convolution, some special functions such as numpy.convolve may be useful later once we understand what you are doing.

Well the code had found it in Matlab and I have guided him, now I show you the code made in Matlab.
vew image
I could run your code in Scilab. Here is a first python equivalent
import numpy as np
from matplotlib import pyplot as plt
N = 40
x = np.ones(N+1)
b = 0.7
S = 5
a =(1 - b) / (1-b**S)
i = np.arange(1,S+1)
 
w = a * (b**(S-i))
# print(w)
y = np.zeros(N)
y[S-1:] = [ np.dot(w, x[n-S+1:n+1].T) for n in range(S-1, N)]
print(y)
print(len(y))
plt.plot(range(S, N+1),y[S-1:], marker='o')
plt.plot(range(S, N+1),y[S-1:], linestyle='-')
plt.show()
(Nov-20-2018, 07:07 PM)Gribouillis Wrote: [ -> ]I could run your code in Scilab. Here is a first python equivalent
import numpy as np
from matplotlib import pyplot as plt
N = 40
x = np.ones(N+1)
b = 0.7
S = 5
a =(1 - b) / (1-b**S)
i = np.arange(1,S+1)
 
w = a * (b**(S-i))
# print(w)
y = np.zeros(N)
y[S-1:] = [ np.dot(w, x[n-S+1:n+1].T) for n in range(S-1, N)]
print(y)
print(len(y))
plt.plot(range(S, N+1),y[S-1:], marker='o')
plt.plot(range(S, N+1),y[S-1:], linestyle='-')
plt.show()

fantastic, thank you very much for doing it saved me from a headache, thank you very much
but I want to know how you did the graph, see if I have the following, how would you do it?
  • x[n] = n, 0 <= n <= 40
I think one only needs to replace line 4 with
x = list(range(N+1))
If you want to go further, you need to analyse the python code to understand how it works.