0

The requirement is to generate a csv file where each column is a data of sinusoidal wave of frequency 1 Hz,2 Hz, 3Hz, 4Hz, 5Hz, 6Hz, 7Hz. Thus total 7 columns.

100 wave points for 1Hertz and thus total 100 x 7= 700 points.

Can anyone explain how to code for this requirement

Uday
  • 57
  • 12

1 Answers1

0

You can try this. Here,

  1. I'm using Numpy to get required values for the frequency.
  2. Then copy these values to pandas dataframe
  3. And finally write it to csv file.

Please refer this link for further useful details regarding sine waves in Python

import numpy as np
import pandas as pd    

def get_values_for_frequency(freq):
    # sampling rate
    sr = 100.0
    # sampling interval
    ts = 1.0/sr
    t = np.arange(0,1,ts)

    # frequency of the signal
    freq = 5   
    y = np.sin(2*np.pi*freq*t)
    return y


df = pd.DataFrame(columns =['1Hz','2Hz', '3Hz', '4Hz', '5Hz', '6Hz', '7Hz'])

df['1Hz']=pd.Series(get_values_for_frequency(1))
df['2Hz']=pd.Series(get_values_for_frequency(2))
df['3Hz']=pd.Series(get_values_for_frequency(3))
df['4Hz']=pd.Series(get_values_for_frequency(4))
df['5Hz']=pd.Series(get_values_for_frequency(5))
df['6Hz']=pd.Series(get_values_for_frequency(6))
df['7Hz']=pd.Series(get_values_for_frequency(7))

df.to_csv('sine.csv', index=False)

Sample output from generated csv file:

enter image description here

Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20
  • 1
    I realized now, all values in a row are same for all columns. You would need to use this as a reference and explore a bit more. Hope it helps. – Manjunath K Mayya Mar 01 '22 at 17:07
  • yeah. I changed and while viewing in Excel for graph representation , it shows only y axis values . I need to show the x axis values as 0 to 1 . How can i add this @Manjunath K Mayya – Uday Mar 02 '22 at 07:10
  • 1
    @Anu: `t = np.arange(0,1,ts)` is the X axis. You can add it as a new column in the dataframe if it is required `df['X-Axis'] = pd.Series(np.arange(0,1,(1/100)))` – Manjunath K Mayya Mar 02 '22 at 14:22
  • Thanks. Is there any way to give separate sampling rate to each column. Like, sr=100 for 1 Hz, sr=200 for 2 Hz ,Sr=300 for 3Hz. If so, does the DataFrame can hold columns of different length? as the sampling rate changes the length of the column. Please clarify my doubt ,Thanks @Manjunath K Mayya – Uday Mar 04 '22 at 12:07
  • 1
    @Anu : You can pass in `sr` as argument for `get_values_for_frequency`. And I think dataframe should support various lengths for column. I guess it will add nan/null values for the columns where values are not present. But you have to try it though. – Manjunath K Mayya Mar 04 '22 at 12:57
  • As you instructed, I passed sr as argument to the function, but the output displays the column of same length in all frequencies. Is there any other ways to give separate separate sampling frequency – Uday Mar 04 '22 at 14:43