Adli Putra
Adli Putra

Reputation: 1

Plotting a signal in Python

I'm trying to plot a simple signal in python, and when i run this it doesn't show any error only 'Restart' and a blank space

from pymatlab import*

import numpy as np

from numpy import sqrt

import matplotlib.pyplot as plt

import scipy as sp

import math

(hashtags) n, coef, freq, phase

def sinyal(N,c,f,p):

 y=np.zeros(N)

 t=np.linspace(0,2*pi,N)

 Nf=len(c)

 for i in range(Nf):

     y+=c[i]*np.sin(f[i]*t)

     return y;

 # Signal Generator

 c=[2,5,10]

 f=[50, 150, 300]

 p=[0,0]

 N=2000

 x=np.linspace(0,2.0*math.pi,N)

 y=sinyal(N,c,f,p)

 plt.plot(x[:100],y[:100])

 plt.show()    

Upvotes: 0

Views: 17146

Answers (2)

unutbu
unutbu

Reputation: 879113

The code you posted has a logical indentation error. The call to sinyal is indented one level, placing it inside the definition of sinyal itself. So although sinyal gets defined, it never gets called.

Using 4 spaces for indentation may help you avoid this error in the future.

Upvotes: 1

xnx
xnx

Reputation: 25478

Your code basically works (apart from some formatting errors and other oddities). I don't have pymatlab but it isn't necessary for this.

import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
import scipy as sp
import math

def sinyal(N,c,f,p):
    y=np.zeros(N)
    t=np.linspace(0,2*np.pi,N)
    Nf=len(c)
    for i in range(Nf):
        y+=c[i]*np.sin(f[i]*t)
    return y;

 # Signal Generator
c=[2,5,10]
f=[50, 150, 300]
p=[0,0]
N=2000
x=np.linspace(0,2.0*math.pi,N)
y=sinyal(N,c,f,p)
plt.plot(x[:100],y[:100])
plt.show()    

Upvotes: 0

Related Questions