0

I want to generate a sinewave with Gaussian noise and save the generated waveform data as a data set. in the next step, I want to find the frequency of the waveform. here is my code(but i don't know why the code isn't working)?

import numpy as np
import scipy as sy
import scipy.fftpack as syfp
import pylab as pyl
import matplotlib.pyplot as plt
import pandas as pd
import sys
import math
import scipy.stats as stats
import serial

dt = 0.03071 
t = dt*np.arange(100000)             ## time at the same spacing of your data
u = np.sin(2*np.pi*t*.01)   
df= pd.dataframe(u, columns=['data'])
np.random.seed(0)
ds = np.random.randn(1000)
rd = [0]
for i in ds:
    rd.append( np.sin(i*0.7 )+ i)
df = pd.DataFrame(rd, columns=['data'])
# Do FFT analysis of array
FFT = sy.fft(u)

# Getting the related frequencies
freqs = syfp.fftfreq(len(u), dt)     ## added dt, so x-axis is in meaningful units

# Create subplot windows and show plot
pyl.subplot(211)
pyl.plot(t, u)
pyl.xlabel('Time')
pyl.ylabel('Amplitude')
pyl.subplot(212)
pyl.plot(freqs, sy.log10(abs(FFT)), '.')  ## it's important to have the abs here
pyl.xlim(-.05, .05)                       ## zoom into see what's going on at the peak
pyl.show()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    "...the code isn't working" is not very descriptive. What is the actual problem with your code? Errors? Wrong output? Please [edit] to include more detail so that we can better understand how to help – G. Anderson Jun 21 '21 at 17:39
  • 1
    Please also isolate the problem. Remove the plotting, the `DataFrame`, etc. If your problem is really just with generating a sinewave with noise, it shouldn't take more than 5 lines and 1 import. – Ilya Jun 21 '21 at 17:46

0 Answers0