Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.0 kB
3
Indexable
Never
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


def generate_sinus_signal(frequency, duration, sampling_rate):
    t = np.arange(0, duration, 1/sampling_rate)
    y = np.sin(2 * np.pi * frequency * t)
    return t, y

#def quantize_signal(signal, bits):
# Perform uniform quantization
#    quantized_signal = np.round((signal + 1) * (2**bits - 1) / 2) / (2**bits - 1) - 1
#    return quantized_signal
    
#def export_to_csv(t, y,quantized_signal, filename):
#    data = {'Time': t, 'F1': y, 'F2': y, 'quantized signal': quantized_signal}
#    df = pd.DataFrame(data)
#    df.to_csv(filename, index=False)   
def export_to_csv(t, y, filename):
    data = {'Time': t, 'F1': y, 'F2': y, 'F1+F2': y}
    df = pd.DataFrame(data)
    df.to_csv(filename, index=False)

def plot_signal(t, y):
    plt.figure(figsize=(10, 4))
    plt.plot(t, y, label='Sinusoidal Signal')
    plt.title('Sinusoidal Signal')
    plt.xlabel('Time (seconds)')
    plt.ylabel('Amplitude')
    plt.legend()
    plt.grid(True)
    plt.show()

# Set the parameters (300Hz fins a 500
frequency = 400.0  # You can change this value to control the frequency
duration = 0.2   # Duration of the signal in seconds
sampling_rate = 10000  # Sampling rate in Hz
quantization_bits = 16  # Number of bits for quantization

# Generate the sinusoidal signal
time, amplitude = generate_sinus_signal(frequency, duration, sampling_rate)

# Quantize the sinusoidal signal
#quantized_amplitude = quantize_signal(amplitude, quantization_bits)

# Plot the sinusoidal signal
#plot_signal(time, amplitude)
#export_to_csv(time, amplitude,quantized_amplitude, 'samples.csv')
export_to_csv(time, amplitude,'samples.csv')
# Plot the quantized sinusoidal signal
#plt.figure(figsize=(12, 6))
plot_signal(time, amplitude)
#plt.plot(time, quantized_amplitude)
#plt.title('Quantized Sinusoidal Signal')
#plt.xlabel('Time (seconds)')
#plt.ylabel('Amplitude')
#plt.legend()
#plt.grid(True)
#plt.show()
Leave a Comment