-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPM_Spettri.py
More file actions
169 lines (140 loc) · 4.43 KB
/
Copy pathCPM_Spettri.py
File metadata and controls
169 lines (140 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import numpy as np
from ImpostazioneSistema.generate_impulse import LREC, LRC
from ImpostazioneSistema.generate_fwaveform import fwaveform
from Modulatore.Modulatore import modulate_signal
from scipy.fft import fft, fftfreq
from scipy.signal import lfilter, butter
from matplotlib import pyplot as plt
def lowpass(x, f_cutoff, fs):
b,a= butter(5, f_cutoff, fs=fs, btype='low', analog=False)
return lfilter(b,a,x)
def set_parameters(j, m, T_symbol, T_sample, L, f_0, M):
with open('modulation_parameter.configurations','wt') as fd:
print('j=',j,file=fd)
print('m=',m,file=fd)
print('T_symbol=',T_symbol,file=fd)
print('T_sample=',T_sample,file=fd)
print('L=',L,file=fd)
print('f_0=',f_0,file=fd)
print('M=',M,file=fd)
alph = np.arange(1,2*M,2)-M
with open('symbols.configurations','wt') as fd:
for el in alph:
print(el, file=fd)
if __name__ == '__main__':
dB = False
# Shared parameters
n_symbols=1e3
T_symbol = 1e-2
f_0 = 1e3
T_sample = 1/(10*f_0)
M=4
windowSize = 200 # filtering window size
modType = LREC
# Gli assi vengono calcolati nella prima simulazione
###################################################
# CPFSK
# h=0.25, L=1
###################################################
j=1
m=4
L=1
set_parameters(j, m, T_symbol, T_sample, L, f_0, M)
fwaveform(modType)
_,s = modulate_signal(n_symbols)
# Assi temporali e delle frequenze
t = np.arange(len(s))*T_sample
f = fftfreq(len(t), T_sample)
window = (f>f_0) & (f<(f_0+3*1/T_symbol))
# window = [True]*len(f)
# Spectra computing:
Sf = fft(s)
PDS = T_symbol / (2**(int(np.log2(len(s)))+1)) * (np.abs(Sf)**2)
PDS = np.array(PDS)
# filtering for a better spectrum --> VBW
b = (1/windowSize)*np.ones(windowSize)
a = 1
PDS = lfilter(b,a,PDS)
if dB:
PDS_dB = 10*np.log10(PDS)
# PDS_dB = PDS_dB - np.max(PDS_dB)
plt.plot(f[window]*T_symbol,PDS_dB[window])
else:
plt.plot(f[window]*T_symbol,PDS[window])
###################################################
# CPFSK
# h=0.5, L=1
###################################################
j=1
m=2
L=1
set_parameters(j, m, T_symbol, T_sample, L, f_0, M)
fwaveform(modType)
_,s = modulate_signal(n_symbols)
# Spectra computing:
Sf = fft(s)
PDS = T_symbol / (2**(int(np.log2(len(s)))+1)) * (np.abs(Sf)**2)
PDS = np.array(PDS)
# filtering for a better spectrum
b = (1/windowSize)*np.ones(windowSize)
a = 1
PDS = lfilter(b,a,PDS)
if dB:
PDS_dB = 10*np.log10(PDS)
# PDS_dB = PDS_dB - np.max(PDS_dB)
plt.plot(f[window]*T_symbol,PDS_dB[window])
else:
plt.plot(f[window]*T_symbol,PDS[window])
###################################################
# CPFSK
# h=0.8, L=1
###################################################
j=4
m=5
L=1
set_parameters(j, m, T_symbol, T_sample, L, f_0, M)
fwaveform(modType)
_,s = modulate_signal(n_symbols)
# Spectra computing:
Sf = fft(s)
PDS = T_symbol / (2**(int(np.log2(len(s)))+1)) * (np.abs(Sf)**2)
PDS = np.array(PDS)
# filtering for a better spectrum
b = (1/windowSize)*np.ones(windowSize)
a = 1
PDS = lfilter(b,a,PDS)
if dB:
PDS_dB = 10*np.log10(PDS)
# PDS_dB = PDS_dB - np.max(PDS_dB)
plt.plot(f[window]*T_symbol,PDS_dB[window])
else:
plt.plot(f[window]*T_symbol,PDS[window])
###################################################
# CPFSK
# h=1, L=1
###################################################
j=1
m=1
L=1
set_parameters(j, m, T_symbol, T_sample, L, f_0, M)
fwaveform(modType)
_,s = modulate_signal(n_symbols)
# Spectra computing:
Sf = fft(s)
PDS = T_symbol / (2**(int(np.log2(len(s)))+1)) * (np.abs(Sf)**2)
# filtering for a better spectrum
b = (1/windowSize)*np.ones(windowSize)
a = 1
PDS = lfilter(b,a,PDS)
# PDS = lowpass(PDS,0.05,1/(f[1]-f[0]))
if dB:
PDS_dB = 10*np.log10(PDS)
# PDS_dB = PDS_dB - np.max(PDS_dB)
plt.plot(f[window]*T_symbol,PDS_dB[window])
else:
plt.plot(f[window]*T_symbol,PDS[window])
# Visualizzazione grafico
plt.title('CPM power density spectra')
plt.xlabel('f T_s')
plt.ylabel('S(f) [dB]')
plt.show()