#-*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft
from scipy.io.wavfile import write

fs = 100e6
f_lo = 10e6
f_in = 2e6
B = 6e6  # 带宽
NFFT = 2000
MFFT = 512
t = np.arange(0, NFFT/fs, 1/fs)
Kr = B / (1/fs*NFFT)
phi1 = np.pi/2
snr = 15
freq_axis = np.arange(0, fs, fs/NFFT)  # 双边带混频 实数混频
s_in = np.random.normal(0, 1, len(t))  # 生成高斯白噪声
s_lo = np.random.normal(0, 1, len(t))  # 生成高斯白噪声
s_mixer1 = s_in * s_lo
s_in_fft = fft(s_in)
s_lo_fft = fft(s_lo)
s_mixer1_fft = fft(s_mixer1)
plt.figure()
plt.subplot(3,1,1)
plt.plot(freq_axis[:NFFT//2]/1e6, 20 * np.log10(np.abs(s_in_fft[:NFFT//2])))
plt.title('subplot 1 : s_in')
plt.xlabel('MHz')
plt.ylabel('dB')
plt.subplot(3,1,2)
plt.plot(freq_axis[:NFFT//2]/1e6, 20 * np.log10(np.abs(s_lo_fft[:NFFT//2])))
plt.title('subplot 2 : s_lo')
plt.xlabel('MHz')
plt.ylabel('dB')
plt.subplot(3,1,3)
plt.plot(freq_axis[:NFFT//2]/1e6, 20 * np.log10(np.abs(s_mixer1_fft[:NFFT//2])))
plt.title('subplot 3 : mix')
plt.xlabel('MHz')
plt.ylabel('dB')
plt.figure()
plt.specgram(s_mixer1, Fs=fs)
plt.title('混频后的 时-频响应')
# s_bw = np.random.normal(0, 1, len(t))
# s_mixer2 = s_bw * s_lo
# f_in_fft = fft(f_in)
# s_bw_fft = fft(s_bw)
# s_lo_fft = fft(s_lo)
# s_mixer2_fft = fft(s_mixer2)
# plt.figure()
# plt.subplot(3,1,1)
# plt.plot(freq_axis[:NFFT//2]/1e6, 20 * np.log10(np.abs(s_bw_fft[:NFFT//2])))
# plt.title('信号 频谱')
# plt.xlabel('MHz')
# plt.ylabel('dB')
# plt.subplot(3,1,2)
# plt.plot(freq_axis[:NFFT//2]/1e6, 20 * np.log10(np.abs(s_lo_fft[:NFFT//2])))
# plt.title('本振 频谱')
# plt.xlabel('MHz')
# plt.ylabel('dB')
# plt.subplot(3,1,3)
# plt.plot(freq_axis[:NFFT//2]/1e6, 20 * np.log10(np.abs(s_mixer2_fft[:NFFT//2])))
# plt.title('混频之后的频谱')
# plt.xlabel('MHz')
# plt.ylabel('dB')
# plt.figure()
# plt.subplot(1,2,1)
# plt.specgram(s_bw, Fs=fs)
# plt.title('混频前的 时-频响应')
# plt.subplot(1,2,2)
# plt.specgram(s_mixer2, Fs=fs)
# plt.title('混频后的 时-频响应')
# plt.show()
