Continue to Site

# Complex Sine Wave File Generation

Status
Not open for further replies.

#### levnu

##### Full Member level 2
Hi,
i would like to create a .hex file or .bin file that will include sine 5MHz wave sample with 320Msps, and i would inject it into FPGA Tx Serdes,
i guess it means 320M samples per second,
does it mean that i need to take a 5MHz signal and sample it each (1/320M) second? for e.g. qty of 5000 samples in a file, how can i do it?
is there a simple generator for this ?

Thanks,

Hi,

320M / 5M = 64
thus you have 64 points of sine in a full wave.

You may use excel to generate the values. 64 lines, just calculate the sine in 360°/64 increments.

Btw:
you did not mention what resolution and format you want the sine values.

Klaus

and yes, I guess there are online tools. Just do an internet search.
Just out of curiosity I did a search: "sine table generator online"
--> 2.7 million hits. I´m a bit speechless, tbh.

You of course don't need that large a table, one with just 90 degrees in it
and appropriate logic/adder can minimize amount of memory needed.
Or DMA with logic for address control to implement the 4 quadrants.

Then there is always CORDIC approach. Although I think that would be too slow
to meet 320 MSPS.

Then there is the resonator approach using two stage IIR approach -

Regards, Dana.

Last edited:

Hi,
i would like to create a .hex file or .bin file that will include sine 5MHz wave sample with 320Msps, and i would inject it into FPGA Tx Serdes,
i guess it means 320M samples per second,
does it mean that i need to take a 5MHz signal and sample it each (1/320M) second? for e.g. qty of 5000 samples in a file, how can i do it?
is there a simple generator for this ?

Thanks,
Hello,

I am doing such things with Python script and Numpy library (from Anaconda).:
Python:
def main():
pass

if __name__ == '__main__':
main()

import numpy as np
import scipy.signal as sig
from scipy.io import wavfile
import matplotlib.pyplot as plt
from fxpmath import Fxp

# sampling frequency
fs = 30000

n = np.arange(8192) #2**13
sinus = 2*np.sin(2 * np.pi * n * 50 / fs)+3*np.sin(2 * np.pi * n * 200 / fs+1.5707)+1.8*np.sin(2 * np.pi * n * 1000 / fs)+0.9*np.sin(2 * np.pi * n * 6500 / fs+0.7853)+2.4*np.sin(2 * np.pi * n * 10000 / fs)+3.7*np.sin(2 * np.pi * n * 12400 / fs)+2*np.sin(2 * np.pi * n * 14800 / fs)

plt.plot(n, sinus)
plt.xlabel('czas [s]')
plt.ylabel('amplituda')
plt.title('Przebieg sygnału sinusoidalnego')
plt.show()

sinus2=sinus.reshape((1,8192)) #trzeba zmienic shape, aby zapis w pliku byl mozliwy
n2=n.reshape((1,8192))

# printing the array and checking datatype
print('Array:', sinus2.shape)
print('Datatype:', sinus2.dtype)

sinus2I=sinus2.astype(np.int64)
print('Array:', sinus2I.shape)
print('Datatype:', sinus2I.dtype)

file = open("C:\\Rob\\AudioSignal.txt", "w")

for x in range(0, 8191):
z = Fxp(sinus2[0][x], signed=True, n_word=16, n_frac=10)
file.write(z.bin()+"\n")
#print(z.hex())   # hex repr
#print(z.val)     # raw val (decimal of binary stored)

file.close()

In this example the signal consist from few sinusoid: first have frequency 50 Hz and amplitude 2, second frequency 200Hz and amplitude 3 and so on ..

In what format you would like to have results:
1) as float numbers
2) as integer
3) fixed point numbers

For example I have been genereting from signal described above .coe file in order to make ROM memory in Xilinx FPGA - such file looks like:

Code:
memory_initialization_radix=2;
memory_initialization_vector=
0000111010001011
0010000011100111
1111011110101011
0001110010011011
0000110000100111
0001010100111101
0001010001111001
0001000111011110
0001001101001010
0000100011001100
0010010110010010
0000011111001011
0000010010101001
0010010000000101
1111010010001000
0010000001001000
0000000011110100
0000001110010111
This is fixed-point with word size 16-bits and fractional part 10-bits.

Give your sampling frequency, ampiltude and frequency of sine signal for which you have sample generated , and give number of samples for 2PI angle. Also declare which format for sample you want (float64,float32,integer,fixed-point) and I can generate file with samples from this Python script for you.

If you want experiment with Python code then you have to install Anaconda with Python interpreter and needed libraries.

Best Regards
--- Updated ---

Hello,

I am doing such things with Python script and Numpy library (from Anaconda).:
Python:
def main():
pass

if __name__ == '__main__':
main()

import numpy as np
import scipy.signal as sig
from scipy.io import wavfile
import matplotlib.pyplot as plt
from fxpmath import Fxp

# sampling frequency
fs = 30000

n = np.arange(8192) #2**13
sinus = 2*np.sin(2 * np.pi * n * 50 / fs)+3*np.sin(2 * np.pi * n * 200 / fs+1.5707)+1.8*np.sin(2 * np.pi * n * 1000 / fs)+0.9*np.sin(2 * np.pi * n * 6500 / fs+0.7853)+2.4*np.sin(2 * np.pi * n * 10000 / fs)+3.7*np.sin(2 * np.pi * n * 12400 / fs)+2*np.sin(2 * np.pi * n * 14800 / fs)

plt.plot(n, sinus)
plt.xlabel('czas [s]')
plt.ylabel('amplituda')
plt.title('Przebieg sygnału sinusoidalnego')
plt.show()

sinus2=sinus.reshape((1,8192)) #trzeba zmienic shape, aby zapis w pliku byl mozliwy
n2=n.reshape((1,8192))

# printing the array and checking datatype
print('Array:', sinus2.shape)
print('Datatype:', sinus2.dtype)

sinus2I=sinus2.astype(np.int64)
print('Array:', sinus2I.shape)
print('Datatype:', sinus2I.dtype)

file = open("C:\\Rob\\AudioSignal.txt", "w")

for x in range(0, 8191):
z = Fxp(sinus2[0][x], signed=True, n_word=16, n_frac=10)
file.write(z.bin()+"\n")
#print(z.hex())   # hex repr
#print(z.val)     # raw val (decimal of binary stored)

file.close()

In this example the signal consist from few sinusoid: first have frequency 50 Hz and amplitude 2, second frequency 200Hz and amplitude 3 and so on ..

In what format you would like to have results:
1) as float numbers
2) as integer
3) fixed point numbers

For example I have been genereting from signal described above .coe file in order to make ROM memory in Xilinx FPGA - such file looks like:

Code:
memory_initialization_radix=2;
memory_initialization_vector=
0000111010001011
0010000011100111
1111011110101011
0001110010011011
0000110000100111
0001010100111101
0001010001111001
0001000111011110
0001001101001010
0000100011001100
0010010110010010
0000011111001011
0000010010101001
0010010000000101
1111010010001000
0010000001001000
0000000011110100
0000001110010111
This is fixed-point with word size 16-bits and fractional part 10-bits.

Give your sampling frequency, ampiltude and frequency of sine signal for which you have sample generated , and give number of samples for 2PI angle. Also declare which format for sample you want (float64,float32,integer,fixed-point) and I can generate file with samples from this Python script for you.

If you want experiment with Python code then you have to install Anaconda with Python interpreter and needed libraries.

Best Regards
BTW: for running Python scripts I am usinhg "Jupyter notebook" or "PyScripter" (on Windows10 OS).

#### Attachments

• PyScripter01.png
184.5 KB · Views: 122
Last edited:

Hello,

I am doing such things with Python script and Numpy library (from Anaconda).:
Python:
def main():
pass

if __name__ == '__main__':
main()

import numpy as np
import scipy.signal as sig
from scipy.io import wavfile
import matplotlib.pyplot as plt
from fxpmath import Fxp

# sampling frequency
fs = 30000

n = np.arange(8192) #2**13
sinus = 2*np.sin(2 * np.pi * n * 50 / fs)+3*np.sin(2 * np.pi * n * 200 / fs+1.5707)+1.8*np.sin(2 * np.pi * n * 1000 / fs)+0.9*np.sin(2 * np.pi * n * 6500 / fs+0.7853)+2.4*np.sin(2 * np.pi * n * 10000 / fs)+3.7*np.sin(2 * np.pi * n * 12400 / fs)+2*np.sin(2 * np.pi * n * 14800 / fs)

plt.plot(n, sinus)
plt.xlabel('czas [s]')
plt.ylabel('amplituda')
plt.title('Przebieg sygnału sinusoidalnego')
plt.show()

sinus2=sinus.reshape((1,8192)) #trzeba zmienic shape, aby zapis w pliku byl mozliwy
n2=n.reshape((1,8192))

# printing the array and checking datatype
print('Array:', sinus2.shape)
print('Datatype:', sinus2.dtype)

sinus2I=sinus2.astype(np.int64)
print('Array:', sinus2I.shape)
print('Datatype:', sinus2I.dtype)

file = open("C:\\Rob\\AudioSignal.txt", "w")

for x in range(0, 8191):
z = Fxp(sinus2[0][x], signed=True, n_word=16, n_frac=10)
file.write(z.bin()+"\n")
#print(z.hex())   # hex repr
#print(z.val)     # raw val (decimal of binary stored)

file.close()

In this example the signal consist from few sinusoid: first have frequency 50 Hz and amplitude 2, second frequency 200Hz and amplitude 3 and so on ..

In what format you would like to have results:
1) as float numbers
2) as integer
3) fixed point numbers

For example I have been genereting from signal described above .coe file in order to make ROM memory in Xilinx FPGA - such file looks like:

Code:
memory_initialization_radix=2;
memory_initialization_vector=
0000111010001011
0010000011100111
1111011110101011
0001110010011011
0000110000100111
0001010100111101
0001010001111001
0001000111011110
0001001101001010
0000100011001100
0010010110010010
0000011111001011
0000010010101001
0010010000000101
1111010010001000
0010000001001000
0000000011110100
0000001110010111
This is fixed-point with word size 16-bits and fractional part 10-bits.

Give your sampling frequency, ampiltude and frequency of sine signal for which you have sample generated , and give number of samples for 2PI angle. Also declare which format for sample you want (float64,float32,integer,fixed-point) and I can generate file with samples from this Python script for you.

If you want experiment with Python code then you have to install Anaconda with Python interpreter and needed libraries.

Best Regards
--- Updated ---

BTW: for running Python scripts I am usinhg "Jupyter notebook" or "PyScripter" (on Windows10 OS).
You sure these techniques would meet the 320,000,000 SPS spec user needs ?

Regards, Dana.

Hello,

see the signal def. : sinus = 2*np.sin(2 * np.pi * n * frequency / fs) where fs is sampling frequency

2 is amplitude and frequency=5Mhz/320MHz signal hence be: 2*sin*(2*PI*n*(5e6/320e6)
2*sin*(2*PI*n*0.015625)

Here is sample file for n=64 (64 samples) - generated as double (Float64):
Code:
0.000000000000000000e+00
9.801714032956060363e-01
1.950903220161282370e+00
2.902846772544623200e+00
3.826834323650897929e+00
4.713967368259975643e+00
5.555702330196021776e+00
6.343932841636455322e+00
7.071067811865474617e+00
7.730104533627369712e+00
8.314696123025450802e+00
8.819212643483549385e+00
9.238795325112867829e+00
9.569403357322089576e+00
9.807852804032304306e+00
9.951847266721967955e+00
1.000000000000000000e+01
9.951847266721969731e+00
9.807852804032304306e+00
9.569403357322089576e+00
9.238795325112867829e+00
8.819212643483551162e+00
8.314696123025454355e+00
7.730104533627370600e+00
7.071067811865475505e+00
6.343932841636455322e+00
5.555702330196021776e+00
4.713967368259978308e+00
3.826834323650898817e+00
2.902846772544624088e+00
1.950903220161286145e+00
9.801714032956082567e-01
1.224646799147353256e-15
-9.801714032956059253e-01
-1.950903220161283702e+00
-2.902846772544620979e+00
-3.826834323650896597e+00
-4.713967368259976531e+00
-5.555702330196014671e+00
-6.343932841636449105e+00
-7.071067811865471064e+00
-7.730104533627367047e+00
-8.314696123025452579e+00
-8.819212643483549385e+00
-9.238795325112864276e+00
-9.569403357322087800e+00
-9.807852804032302529e+00
-9.951847266721969731e+00
-1.000000000000000000e+01
-9.951847266721969731e+00
-9.807852804032304306e+00
-9.569403357322089576e+00
-9.238795325112866053e+00
-8.819212643483551162e+00
-8.314696123025454355e+00
-7.730104533627368824e+00
-7.071067811865477282e+00
-6.343932841636458875e+00
-5.555702330196021776e+00
-4.713967368259979196e+00
-3.826834323650904146e+00
-2.902846772544624976e+00
-1.950903220161287255e+00
-9.801714032956050371e-01

with such python script:

Python:
def main():
pass

if __name__ == '__main__':
main()

import numpy as np
import scipy.signal as sig
from scipy.io import wavfile
import matplotlib.pyplot as plt
from fxpmath import Fxp

# częstotliwość próbkowania
fs = 320e6

n = np.arange(64) #2**13
sinus = 10*np.sin(2 * np.pi * n * 5e6 / fs)

plt.plot(n, sinus)
plt.xlabel('czas [s]')
plt.ylabel('amplituda')
plt.title('Przebieg sygnału sinusoidalnego')
plt.show()

sinus2=sinus.reshape((1,64)) #trzeba zmienic shape, aby zapis w pliku byl mozliwy

# printing the array and checking datatype
print('Array:', sinus2.shape)
print('Datatype:', sinus2.dtype)

file = open("C:\\Rob\\Sine5MHzSignal.txt", "w")

for row in sinus2:
np.savetxt(file,row)
file.close()

There is very simple to do changing output format as Float32, Integer64/32 or fixed-point binary or hex values.

This is just simple math, not magic

Best Regards
--- Updated ---

BTW: see this link related to calculating FFT on audio signal (my link on other forum). I don't know if it is allowed to place post from different forums - if not let administrator take proper action>

https://www.eevblog.com/forum/fpga/xilinx-fft-ip-core-window-filtering-function-is-not-implemented/

Best Regards

Last edited:

Hi,

yes: VB, VBscript, C, and many other ways...

you say amplitude is 2 but the values show an amplitude of 10.

Klaus

Hello @KlausST,

see code of last Python script:

Code:
 sinus = 10*np.sin(2 * np.pi * n * 5e6 / fs)

Amplitude there is 10.

Best Regards

Hello,

I am doing such things with Python script and Numpy library (from Anaconda).:
Python:
def main():
pass

if __name__ == '__main__':
main()

import numpy as np
import scipy.signal as sig
from scipy.io import wavfile
import matplotlib.pyplot as plt
from fxpmath import Fxp

# sampling frequency
fs = 30000

n = np.arange(8192) #2**13
sinus = 2*np.sin(2 * np.pi * n * 50 / fs)+3*np.sin(2 * np.pi * n * 200 / fs+1.5707)+1.8*np.sin(2 * np.pi * n * 1000 / fs)+0.9*np.sin(2 * np.pi * n * 6500 / fs+0.7853)+2.4*np.sin(2 * np.pi * n * 10000 / fs)+3.7*np.sin(2 * np.pi * n * 12400 / fs)+2*np.sin(2 * np.pi * n * 14800 / fs)

plt.plot(n, sinus)
plt.xlabel('czas [s]')
plt.ylabel('amplituda')
plt.title('Przebieg sygnału sinusoidalnego')
plt.show()

sinus2=sinus.reshape((1,8192)) #trzeba zmienic shape, aby zapis w pliku byl mozliwy
n2=n.reshape((1,8192))

# printing the array and checking datatype
print('Array:', sinus2.shape)
print('Datatype:', sinus2.dtype)

sinus2I=sinus2.astype(np.int64)
print('Array:', sinus2I.shape)
print('Datatype:', sinus2I.dtype)

file = open("C:\\Rob\\AudioSignal.txt", "w")

for x in range(0, 8191):
z = Fxp(sinus2[0][x], signed=True, n_word=16, n_frac=10)
file.write(z.bin()+"\n")
#print(z.hex())   # hex repr
#print(z.val)     # raw val (decimal of binary stored)

file.close()

In this example the signal consist from few sinusoid: first have frequency 50 Hz and amplitude 2, second frequency 200Hz and amplitude 3 and so on ..

In what format you would like to have results:
1) as float numbers
2) as integer
3) fixed point numbers

For example I have been genereting from signal described above .coe file in order to make ROM memory in Xilinx FPGA - such file looks like:

Code:
memory_initialization_radix=2;
memory_initialization_vector=
0000111010001011
0010000011100111
1111011110101011
0001110010011011
0000110000100111
0001010100111101
0001010001111001
0001000111011110
0001001101001010
0000100011001100
0010010110010010
0000011111001011
0000010010101001
0010010000000101
1111010010001000
0010000001001000
0000000011110100
0000001110010111
This is fixed-point with word size 16-bits and fractional part 10-bits.

Give your sampling frequency, ampiltude and frequency of sine signal for which you have sample generated , and give number of samples for 2PI angle. Also declare which format for sample you want (float64,float32,integer,fixed-point) and I can generate file with samples from this Python script for you.

If you want experiment with Python code then you have to install Anaconda with Python interpreter and needed libraries.

Best Regards
--- Updated ---

BTW: for running Python scripts I am usinhg "Jupyter notebook" or "PyScripter" (on Windows10 OS).

my FPGA guy gave me these instructions:
Hex data format:

1. File per channel

2. Each line - 4 bytes

4. Each line we have 4 bytes

0x01020304

0x05060708

0x090A0B0C

......

Hi,

are you asking for some one else to modify / write the code for you?
This is not how a forum is meant to work.

Klaus

Hi,

are you asking for some one else to modify / write the code for you?
This is not how a forum is meant to work.

Klaus
No, sorry i think you miss understood, i answered to the person that asked me in what format im looking to represent it, btw currently i did next function that migh be good:
fs = 320e6; % Sampling frequency (samples per second)
dt = 1/fs; % seconds per sample
StopTime = 0.000010; % seconds
t = (0:dt:StopTime-dt)'; % seconds
F = 10e6; % Sine wave frequency (hertz)

data_I = cos(2*pi*F*t);
data_Q = sin(2*pi*F*t);
plot(data_I(2:end,1))
xlabel('Time index n');
ylabel('Amplitude');

title('Sine Wave');
grid on
hold on
plot(data_Q(2:end,1))

% HEX_I=num2hex(data_I);
% HEX_Q=num2hex(data_Q);

HEX_16_I=dec2hex(typecast(data_I,'uint32'),8);
HEX_16_Q=dec2hex(typecast(data_Q,'uint32'),8);

No, sorry i think you miss understood, i answered to the person that asked me in what format im looking to represent it, btw currently i did next function that migh be good:
fs = 320e6; % Sampling frequency (samples per second)
dt = 1/fs; % seconds per sample
StopTime = 0.000010; % seconds
t = (0:dt:StopTime-dt)'; % seconds
F = 10e6; % Sine wave frequency (hertz)

data_I = cos(2*pi*F*t);
data_Q = sin(2*pi*F*t);
plot(data_I(2:end,1))
xlabel('Time index n');
ylabel('Amplitude');

title('Sine Wave');
grid on
hold on
plot(data_Q(2:end,1))

% HEX_I=num2hex(data_I);
% HEX_Q=num2hex(data_Q);

HEX_16_I=dec2hex(typecast(data_I,'uint32'),8);
HEX_16_Q=dec2hex(typecast(data_Q,'uint32'),8);
Hello @levnu,
thanks for clarification.

Best Regards

Hello @levnu,
thanks for clarification.

Best Regards
BTW it's working, when I tried to reconstruct I didn't get sine wave again

Status
Not open for further replies.