|
|
|
@ -16,44 +16,39 @@ if __name__ == "__main__":
|
|
|
|
graph = 'y'
|
|
|
|
graph = 'y'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create a sample signal (sum of two sine waves)
|
|
|
|
sampling_rate = 1024e3 # 250e3 # Hz
|
|
|
|
sampling_rate = 1024e3 # 250e3 # Hz
|
|
|
|
duration = 65536/sampling_rate # 1 # seconds
|
|
|
|
duration = 65536/sampling_rate # 1 # seconds
|
|
|
|
t = np.linspace(0, duration, int(sampling_rate * duration), endpoint=False)
|
|
|
|
# t = np.linspace(0, duration, int(sampling_rate * duration), endpoint=False)
|
|
|
|
|
|
|
|
t = np.linspace(0, duration, int(sampling_rate * duration), endpoint=False)
|
|
|
|
|
|
|
|
# frequency1 = 50 # Hz
|
|
|
|
|
|
|
|
# frequency2 = 120 # Hz
|
|
|
|
|
|
|
|
# signal = 0.7 * np.sin(2 * np.pi * frequency1 * t) + np.sin(2 * np.pi * frequency2 * t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sdr = RtlSdr()
|
|
|
|
sdr = RtlSdr()
|
|
|
|
|
|
|
|
|
|
|
|
# configure device
|
|
|
|
# configure device
|
|
|
|
sdr.sample_rate = sampling_rate # 250e3 # 2.4e6
|
|
|
|
sdr.sample_rate = sampling_rate # 250e3 # 2.4e6
|
|
|
|
#center_frequency = 434.8e6
|
|
|
|
#center_frequency = 434.8e6
|
|
|
|
sdr.center_freq = center_frequency
|
|
|
|
sdr.center_freq = center_frequency
|
|
|
|
sdr.gain = 4
|
|
|
|
sdr.gain = 4
|
|
|
|
sdr.direct_sampling = False
|
|
|
|
sdr.direct_sampling = False
|
|
|
|
|
|
|
|
|
|
|
|
# signal = sdr.read_samples(64*1024) #256
|
|
|
|
# signal = sdr.read_samples(64*1024) #256
|
|
|
|
signal = sdr.read_samples(duration*sampling_rate).real #256
|
|
|
|
signal = sdr.read_samples(duration*sampling_rate).real #256
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Center frequency is {center_frequency}")
|
|
|
|
print(f"Center frequency is {center_frequency}")
|
|
|
|
|
|
|
|
|
|
|
|
sdr.close()
|
|
|
|
sdr.close()
|
|
|
|
|
|
|
|
|
|
|
|
# Compute the FFT
|
|
|
|
# Compute the FFT
|
|
|
|
fft_result = np.fft.fft(signal)
|
|
|
|
fft_result = np.fft.fft(signal)
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate the frequencies corresponding to the FFT output
|
|
|
|
# Calculate the frequencies corresponding to the FFT output
|
|
|
|
n = len(signal)
|
|
|
|
n = len(signal)
|
|
|
|
frequencies = np.fft.fftfreq(n, d=1/sampling_rate)
|
|
|
|
frequencies = np.fft.fftfreq(n, d=1/sampling_rate)
|
|
|
|
|
|
|
|
|
|
|
|
# Take the absolute value for amplitude spectrum and consider only the positive frequencies
|
|
|
|
# Take the absolute value for amplitude spectrum and consider only the positive frequencies
|
|
|
|
positive_frequencies_indices = np.where(frequencies >= 0)
|
|
|
|
positive_frequencies_indices = np.where(frequencies >= 0)
|
|
|
|
positive_frequencies = frequencies[positive_frequencies_indices]
|
|
|
|
positive_frequencies = frequencies[positive_frequencies_indices]
|
|
|
|
amplitude_spectrum = 2/n * np.abs(fft_result[positive_frequencies_indices]) # Normalize for amplitude
|
|
|
|
amplitude_spectrum = 2/n * np.abs(fft_result[positive_frequencies_indices]) # Normalize for amplitude
|
|
|
|
|
|
|
|
|
|
|
|
if (graph == 'y'):
|
|
|
|
if (graph == 'y'):
|
|
|
|
# Plotting the results
|
|
|
|
# Plotting the results
|
|
|
|
plt.figure(figsize=(12, 6))
|
|
|
|
plt.figure(figsize=(12, 6))
|
|
|
|
|
|
|
|
|
|
|
|
@ -73,24 +68,24 @@ if (graph == 'y'):
|
|
|
|
plt.tight_layout()
|
|
|
|
plt.tight_layout()
|
|
|
|
plt.show()
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
# print(amplitude_spectrum)
|
|
|
|
# print(amplitude_spectrum)
|
|
|
|
x = amplitude_spectrum
|
|
|
|
x = amplitude_spectrum
|
|
|
|
# print(x)
|
|
|
|
# print(x)
|
|
|
|
min_value = min(x)
|
|
|
|
min_value = min(x)
|
|
|
|
max_value = max(x)
|
|
|
|
max_value = max(x)
|
|
|
|
|
|
|
|
|
|
|
|
#freq_min = np.argmax(min_value)
|
|
|
|
#freq_min = np.argmax(min_value)
|
|
|
|
# print(np.argmax(x))
|
|
|
|
# print(np.argmax(x))
|
|
|
|
# print(np.argmax(x)*(150e3 - 10e3)/(9770 - 709))
|
|
|
|
# print(np.argmax(x)*(150e3 - 10e3)/(9770 - 709))
|
|
|
|
# print(sampling_rate)
|
|
|
|
# print(sampling_rate)
|
|
|
|
# print(center_frequency)
|
|
|
|
# print(center_frequency)
|
|
|
|
|
|
|
|
|
|
|
|
offset = (np.argmax(x)*(150e3 - 10e3)/(9770 - 709))
|
|
|
|
offset = (np.argmax(x)*(150e3 - 10e3)/(9770 - 709))
|
|
|
|
freq_max = center_frequency + offset
|
|
|
|
freq_max = center_frequency + offset
|
|
|
|
|
|
|
|
|
|
|
|
print(f" {freq_max} {max_value}")
|
|
|
|
print(f" {freq_max} {max_value}")
|
|
|
|
#print(f"The minimum signal is {min_value} at frequency {freq_min}")
|
|
|
|
#print(f"The minimum signal is {min_value} at frequency {freq_min}")
|
|
|
|
|
|
|
|
|
|
|
|
#print(min_value)
|
|
|
|
#print(min_value)
|
|
|
|
#print(max_value)
|
|
|
|
#print(max_value)
|
|
|
|
|
|
|
|
|
|
|
|
|