*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit 0e8b0974 authored by Schabbauer, Johannes's avatar Schabbauer, Johannes
Browse files

-added function to output multiple tones

parent 7eb0cd80
No related branches found
No related tags found
1 merge request!2Added new spectrum awg card.
......@@ -515,9 +515,7 @@ class SpectrumCard:
# pnBuffer[i] = np.int16(np.sin(2 * np.pi * frequency * t[i]) * 32766)
# return pnBuffer
def generate_single_tone(frequency, num_samples):
sample_rate = 1.25e9 # Sampling rate (1.25 GHz)
#num_samples = 1*4096 #int(duration * sample_rate)
def generate_single_tone(frequency, num_samples, sample_rate = 1.25e9 ):
duration = num_samples/sample_rate # Duration to cover one cycle of the sine wave, in seconds
# Calculate the number of samples needed for one cycle
......@@ -525,14 +523,54 @@ def generate_single_tone(frequency, num_samples):
# Generate time values
t = np.linspace(0, duration, num_samples, endpoint=False)
return np.int16(np.sin(2 * np.pi * frequency * t) * 32766)
return np.int16(np.sin(2 * np.pi * frequency * t) * 32767)
def generate_multi_tone(frequencies, amplitudes, num_samples, sample_rate=1.25e9):
"""
Generates a signal that is the sum of multiple sine waves, with normalization to prevent overflow.
Parameters:
- frequencies: A list of frequencies for the sine waves.
- amplitudes: A list of amplitudes for the sine waves. Must be the same length as frequencies.
- num_samples: The total number of samples in the generated signal.
- sample_rate: The sample rate in Hz.
Returns:
- An array containing the generated signal, normalized and cast to int16.
"""
if len(frequencies) != len(amplitudes):
raise ValueError("Frequencies and amplitudes lists must have the same length.")
# Duration to cover the desired number of samples
duration = num_samples / sample_rate
# Generate time values
t = np.linspace(0, duration, num_samples, endpoint=False)
# Initialize the signal with zeros
signal = np.zeros(num_samples)
# Accumulate sine waves for each frequency and amplitude pair
for frequency, amplitude in zip(frequencies, amplitudes):
signal += amplitude * np.sin(2 * np.pi * frequency * t)
# Normalize the signal to the range [-32767, 32767] to prevent overflow when casting to int16
# Find the peak value
peak_value = np.abs(signal).max()
if peak_value > 0:
# Normalize signal
normalization_factor = 32767 / peak_value
signal = signal * normalization_factor
return np.int16(signal)
# Main Code
sample_rate = MEGA(1250)
tweezerAWG = SpectrumCard('/dev/spcm0')
tweezerAWG.set_clock('external',MEGA(10))
tweezerAWG.set_sample_rate(MEGA(1250))
tweezerAWG.set_sample_rate(sample_rate)
tweezerAWG.set_channel_status(0,True)
tweezerAWG.set_channel_status(0,True)
tweezerAWG.set_channel_enable(0,True)
......@@ -541,9 +579,15 @@ tweezerAWG.set_channel_filter(0,False)
tweezerAWG.set_channel_mode(0,'double')
tweezerAWG.set_generation_mode(mode='single')
tweezerAWG.set_loops(0)
frequency = KILO(305.175)
num_samples = 4096
samples = generate_single_tone(frequency, num_samples)
frequency = KILO(305.175*100)
num_samples = 4096*50
#samples = generate_single_tone(frequency, num_samples)
fundamental_frequency = sample_rate/num_samples
print(fundamental_frequency)
frequencies = np.round(np.linspace(16000,33000,200))*fundamental_frequency
print(frequencies/1e6)
samples = generate_multi_tone(frequencies,np.ones(len(frequencies)),num_samples)
tweezerAWG.transfer_samples(samples)
# Plot the generated sine wave
plt.figure(figsize=(10, 4))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment