import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
import random
fs=13560000
bit_0=[-1]*64+[1]*64
bit_1=[1]*64+[-1]*64
CEF_SOF=bit_0*48
CEF_EOF=[1]*512
cef_frame_length=16
cef_frame_bit=[]
cef_frame_pattern=[]
for i in range(cef_frame_length):
cef_frame_bit.append(random.choice([0,1]))
cef_frame_bit=[1,0,1,1,0,0,1,0,0,1,0,0,1,1,0,1]+cef_frame_bit #add B24D
cef_frame_pattern=cef_frame_pattern+CEF_SOF
cnt=0
for i in range(cef_frame_length):
if cef_frame_bit[i]==0:
cef_frame_pattern=cef_frame_pattern+bit_0
else:
cef_frame_pattern=cef_frame_pattern+bit_1
cef_frame_pattern=cef_frame_pattern+CEF_EOF
#212k analysis
cef_frame_pattern_dwon_sample=[]
for i in range(len(cef_frame_pattern)):
if i%2==0:
cef_frame_pattern_dwon_sample.append(cef_frame_pattern[i])
# FFT analysis
fft_result=np.fft.fft(np.array(cef_frame_pattern_dwon_sample))
freqs=np.fft.fftfreq(len(fft_result),1/fs)
length_fft=len(fft_result)//2
# find 95% energy frequency distribution
energy_threshold = 0.95 * np.sum(np.abs(fft_result[1:length_fft])**2)
cumulative_energy = 0
freq_range = []
for i in range(len(fft_result)//2):
if i==0:
continue
if cumulative_energy < energy_threshold:
freq_range.append(freqs[i])
cumulative_energy += np.abs(fft_result[i])**2
plt.figure(figsize=(12,6))
plt.subplot(2,1,1)
plt.plot(cef_frame_pattern_dwon_sample,marker='.')
plt.title("CEF 212k signal")
plt.subplot(2,1,2)
plt.plot(freqs[:length_fft],np.abs(fft_result)[:length_fft])
plt.axvline(freq_range[0], color='r', linestyle='--', label='95% Energy Range')
plt.axvline(freq_range[-1], color='r', linestyle='--')
plt.title("Fequency Spectrum")
plt.xlabel("Fequency(Hz))")
plt.tight_layout()
#424k analysis
cef_frame_pattern_dwon_sample=[]
for i in range(len(cef_frame_pattern)):
if i%4==0:
cef_frame_pattern_dwon_sample.append(cef_frame_pattern[i])
# FFT analysis
fft_result=np.fft.fft(np.array(cef_frame_pattern_dwon_sample))
freqs=np.fft.fftfreq(len(fft_result),1/fs)
length_fft=len(fft_result)//2
# find 95% energy frequency distribution
energy_threshold = 0.95 * np.sum(np.abs(fft_result[1:length_fft])**2)
cumulative_energy = 0
freq_range = []
for i in range(len(fft_result)//2):
if i==0:
continue
if cumulative_energy < energy_threshold:
freq_range.append(freqs[i])
cumulative_energy += np.abs(fft_result[i])**2
plt.figure(figsize=(12,6))
plt.subplot(2,1,1)
plt.plot(cef_frame_pattern_dwon_sample,marker='.')
plt.title("CEF 424k signal")
plt.subplot(2,1,2)
plt.plot(freqs[:length_fft],np.abs(fft_result)[:length_fft])
plt.axvline(freq_range[0], color='r', linestyle='--', label='95% Energy Range')
plt.axvline(freq_range[-1], color='r', linestyle='--')
plt.title("Fequency Spectrum")
plt.xlabel("Fequency(Hz))")
plt.tight_layout()
plt.show()
标签:cef,plt,NFC,pattern,frame,fft,np,TYPE,PICC
From: https://blog.csdn.net/u013912838/article/details/139210322