Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to plot graph sine wave

I have this signal :

from math import*
Fs=8000
f=500
sample=16
a=[0]*sample
for n in range(sample):
    a[n]=sin(2*pi*f*n/Fs)

How can I plot a graph (this sine wave)?

and create name of xlabel as 'voltage(V)' and ylabel as 'sample(n)'

What code to do this?

I am so thanksful for help ^_^

like image 234
Wittaya Avatar asked Mar 21 '14 18:03

Wittaya


People also ask

How do I generate a sine wave in matplotlib?

Here is the code to generate sine wave in Matplotlib. The graph of y=sin(x) y = sin ( x ) for x between −π and π .

How do you graph a sine wave?

To graph the sine function, we mark the angle along the horizontal x axis, and for each angle, we put the sine of that angle on the vertical y-axis. The result, as seen above, is a smooth curve that varies from +1 to -1. Curves that follow this shape are called 'sinusoidal' after the name of the sine function.


1 Answers

  • Setting the x-axis with np.arange(0, 1, 0.001) gives an array from 0 to 1 in 0.001 increments.
    • x = np.arange(0, 1, 0.001) returns an array of 1000 points from 0 to 1, and y = np.sin(2*np.pi*x) you will get the sin wave from 0 to 1 sampled 1000 times

I hope this will help:

import matplotlib.pyplot as plt
import numpy as np


Fs = 8000
f = 5
sample = 8000
x = np.arange(sample)
y = np.sin(2 * np.pi * f * x / Fs)
plt.plot(x, y)
plt.xlabel('sample(n)')
plt.ylabel('voltage(V)')
plt.show()

enter image description here

P.S.: For comfortable work you can use The Jupyter Notebook.

like image 119
nikioa Avatar answered Oct 16 '22 05:10

nikioa