0
$\begingroup$

I need to produce a signal of $\sin(2\pi\cdot 60\cdot10^6 t)$, where $t$ in sec, with sample rate of $2^{28}$ Hz.

Since the signal has period of $\frac{1}{60\cdot 10^6}$ sec can I somehow sample only few samples of 1 period and continuously play it many times ?

My problem is the transfer of memory from computer to device.

Thanks

$\endgroup$

1 Answer 1

0
$\begingroup$

First of all the sample rate that you have provided is unreasonably large. Usual audio sampling frequencies are either $44,100 \:\texttt{kHz}$ or $48,000 \:\texttt{kHz}$, for example.

Secondly, yes, what you have identified here is an approach that is commonly used in digital signal processing to save memory and computational resources.

Here is my implementation at a lower (audible) frequency with a reasonable sampling rate:

import numpy as np
import pyaudio


frequency = 1000  #Freq in Hz
sample_rate = 48000  # Reasonable Sample rate
period = 1 / frequency
num_samples_per_period = int(period * sample_rate)

# Generate one period of sine wave
t = np.arange(0, period, 1/sample_rate)
sine_wave = np.sin(2 * np.pi * frequency * t)


p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=int(sample_rate),
                output=True)

# Infinite loop
while True:
    stream.write(sine_wave.astype(np.float32).tobytes())

stream.stop_stream()
stream.close()
p.terminate()

You can confirm that the correct signal is being generated correctly using this website. Also don't forget to pip install pyaudio

$\endgroup$
4
  • $\begingroup$ First, thank you for your code but I'm using Matlab. The example you put is for frequency of 1000 and I need to generate 60M Hz, so 48K will not work for me, and I'm using spectrum-instrumentation.com/products/… to generate the analog signal so to make it more smooth I need high sample rate $\endgroup$
    – Yar Sha
    Commented Aug 10, 2023 at 11:57
  • $\begingroup$ Nyquist Rate for 60 MHz is 120 MHz. To be safe you can sample at 125MHz which is supported by the products you have linked. Also, $2^{28} \approx 268.4$ MHz which is again unreasonably large. $\endgroup$ Commented Aug 10, 2023 at 13:20
  • $\begingroup$ My frequency is $2^{26} = 67108864$ so if I will use sample rate of $2^{27} =$ 125MHz. It yields - $sin(2 \pi \cdot 2^{26} \frac{k}{2^{27}} )= sin(2 \pi \cdot \frac{k}{2} )$ which means only 2 sample in period for k = 0, 1 . It means I will generate only the values 0,1 . it's not make sense to be smooth enough for my purpose. If I will use sample rate of $2^{28}$ I will generate the samples 0,1,0,-1 which is still not smooth enough in my opinion but make more sense... Am I wrong? $\endgroup$
    – Yar Sha
    Commented Aug 10, 2023 at 13:44
  • $\begingroup$ While the example shows a reasonable idea, the example values used may lead to poor results. As 1 kHz sine wave at 48 kHz sampling rate has exactly 48 samples per period, in any reasonable audio setting, repeating only 48 samples over and over is an extremely poor choise. It will serve for simple purposes, but for any serious work, the sine frequency should be relatively prime to the sampling rate, for example 997 Hz at 48 kHz. $\endgroup$
    – Justme
    Commented Aug 11, 2023 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.