-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOFDM_MWC.m
225 lines (179 loc) · 6.41 KB
/
OFDM_MWC.m
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
clear all;
close all;
clc;
%% Parameters
PPM = 20;
R = 1e5; % [bits/sec](改变点的个数)
duration = 0.128; % [sec]
DataL = R*duration/2; % Data length in symbols 符号数据长度
NFFT = 128; %初始值128
M = 4; % 4 QAM
snr = 20; % [dB]
Nsym = 4; % Filter order in symbol durations 初始值为4
beta = 0.5; % Roll-off factor 滚降系数
sampsPerSym = 10; % Upsampling factor 升频系数(可改变频带大小) 初始值为10
L = sampsPerSym*Nsym + 1; % Raised cosine filter order 升余弦滤波器
Fs = R * sampsPerSym; % Sampling frequency 取样频率
cyclic_prefix_signal = [];
%% Raised cosine filter design
shape = 'Raised Cosine';
% Specifications of the raised cosine filter with given order in symbols
rcosSpec = fdesign.pulseshaping(sampsPerSym, shape, 'Nsym,beta', Nsym, beta);
rcosFlt = design(rcosSpec);
rcosFlt.Numerator = rcosFlt.Numerator / max(rcosFlt.Numerator);
signal=(zeros(((NFFT+L)/NFFT)*DataL*sampsPerSym,1))';
fhz = [Fs/5,Fs/2.5];
for i=1:2
%% Tranceiver
bernoulli_binary_generator = randint(R*duration,1);
% Transmiter
bernoulli_two_samples = reshape(bernoulli_binary_generator, length(bernoulli_binary_generator)/2, 2);
dec = bi2de(bernoulli_two_samples,'left-msb'); % Bit to integer
modulated_data = qammod(dec,M); % 4 QAM 星座上的图
% figure(3)
% I = real(modulated_data);
% Q = imag(modulated_data);
% subplot(211);
% x=1:100;
% plot(x,Q(1:100),'rs');
% title('4QAM调制后的虚部');
% subplot(212);
% x=1:100;
% plot(x,I(1:100),'bd');
% title('4QAM调制后的实部');
% Serial to Parallel 连续到并行
cyclic_prefix_signal = [];
for id = 1:length(modulated_data)/NFFT
ifft_signal = ifft(modulated_data((id-1)*NFFT+1:id*NFFT)); % ifft
% adding cyclic prefix 加入循环前缀
cyclic_prefix = zeros(NFFT + L, 1);
cyclic_prefix(1:L) = ifft_signal(end-L+1:end);
cyclic_prefix(L+1:end) = ifft_signal;
cyclic_prefix_signal = [cyclic_prefix_signal; cyclic_prefix];
end;
%加入循环前缀以后的信号
% figure(5)
% x=1:length(cyclic_prefix_signal);
% y1=abs(fft(cyclic_prefix_signal,length(cyclic_prefix_signal)));
% plot(x(1:length(cyclic_prefix_signal)/2),y1(1:length(cyclic_prefix_signal)/2));
% title('ifft后传输信号频谱');
% D/A
signal_complex = filter(rcosFlt, upsample([cyclic_prefix_signal; zeros(Nsym/2,1)], sampsPerSym));
fltDelay = Nsym / (2*R); % Filter group delay, since raised cosine filter is linear phase and symmetric.滤波器组延迟,因为升余弦滤波器是线性相位和对称的。
signal_complex = signal_complex(fltDelay*Fs+1:end); % Correct for propagation delay by removing filter transients 通过去除过滤瞬变,正确地传播延迟
t = (0: length(signal_complex) - 1) / Fs;
f = linspace(-Fs/2,Fs/2,length(signal_complex));%生成线性间隔矢量
I = real(signal_complex);
Q = imag(signal_complex);
FI = fftshift(fft(I));
FQ = fftshift(fft(Q));
a=I'.*cos(2*pi*fhz(i)*t);
b=Q'.*sin(2*pi*fhz(i)*t);
signal=signal+(a-b);
% signal= signal+(I'.*cos(2*pi*fhz(i)*t) - Q'.*sin(2*pi*fhz(i)*t));
end;
figure(4)
x=1:length(signal);
y1=abs(fft(signal,length(signal)));
plot(x(1:length(signal)/2),y1(1:length(signal)/2));
title('接收信号频谱');
figure(5)
x=1:length(signal);
plot(x,signal);
title('接收信号时域谱');
% figure(3);
% plot(f,abs(signal));
% Channel
% noised_signal = awgn(signal,snr,'measured'); % Adding white gaussian noise
noised_signal = signal;
% Reciever
%生成了两个OFDM信号相加的信号
%选择是否加窗
%%数据初始化
ChannelNum = 50; %MWC通道数
N=40;
L=35; %数目预警
M=35;
L0=floor(M/2);
TimeResolution=duration/length(noised_signal);
t_axis=0:TimeResolution:duration-TimeResolution;
fnyq=1/TimeResolution;
fp=fnyq/L;
Tp=1/fp;
m=ChannelNum;
K=81;
R=1;
%生成+-1伪随机序列
SignPatterns = randsrc(m,M);
%%混合信号
MixedSigSequences = zeros(m,length(t_axis));
for channel=1:m
MixedSigSequences(channel,:) = MixSignal(noised_signal,t_axis,SignPatterns(channel,:),Tp);
end
% figure(4)
% signal=MixedSigSequences(3,:)
% x=1:length(signal);
% y1=abs(fft(signal,length(signal)));
% plot(x(1:length(signal)/2),y1(1:length(signal)/2));
% title('其中一个支路信号频谱');
%% Analog low-pass filtering and actual sampling
% fprintf(1,'Filtering and decimation (=sampling)\n');
% ideal pass filter
temp = zeros(1,K);
temp(1) = 1;
lpf_z = interpft(temp,length(t_axis))/R/L; % impulse response
SignalSampleSequences = zeros(m,K);
NoiseSampleSequences = zeros(m,K);
% fprintf(1,' Channel ');
decfactor = L*R;
for channel = 1:m
% fprintf(1,'.'); if ( (mod(channel,5)==0) || (channel==m)) fprintf(1,'%d',channel); end
SignalSequence = MixedSigSequences(channel,:);
% NoiseSequence = MixedNoiseSequences(channel,:);
DigitalSignalSamples(channel, :) = FilterDecimate(SignalSequence,decfactor,lpf_z);
% DigitalNoiseSamples(channel, :) = FilterDecimate(NoiseSequence,decfactor,lpf_z);
end
Digital_time_axis = downsample(t_axis,decfactor);
DigitalLength = length(Digital_time_axis);
%% CTF block
% fprintf(1,'---------------------------------------------------------------------------------------------\n');
% fprintf(1,'Entering CTF block\n');
% define matrices for fs=fp
S = SignPatterns;
theta = exp(-j*2*pi/L);
F = theta.^([0:L-1]'*[-L0:L0]);
np = 1:L0;
nn = (-L0):1:-1;
% This is for digital input only. Note that when R -> infinity,
% D then coincides with that of the paper
dn = [ (1-theta.^nn)./(1-theta.^(nn/R))/(L*R) 1/L (1-theta.^np)./(1-theta.^(np/R))/(L*R)];
D = diag(dn);
A1 = S*F*D;
A = conj(A1);
DigitalSamples = DigitalSignalSamples;
% Frame construction
Q = DigitalSamples* DigitalSamples';
% decompose Q to find frame V
NumDomEigVals= FindNonZeroValues(eig(Q),5e-8);
[V,d] = eig_r(Q,min(NumDomEigVals,2*N));%V是对应于最大特征值所在位置的特征向量;d是最大的2*N个特征值
v = V*diag(sqrt(d));
% N iterations at most, since we force symmetry in the support...
[u, RecSupp] = RunOMP_Unnormalized(v, A, N, 0, 0.01, true);
RecSuppSorted = sort(unique(RecSupp));
%% Recover the singal
A_S = A(:,RecSuppSorted);
hat_zn = pinv(A_S)*DigitalSamples; % inverting A_S
hat_zt = zeros(size(hat_zn,1),length(x));
for ii = 1:size(hat_zt,1) % interpolate (by sinc)
hat_zt(ii,:) = interpft(hat_zn(ii,:),L*R*length(hat_zn(ii,:)));
end
x_rec = zeros(1,length(x));
for ii = 1:size(hat_zt,1) % modulate each band to their corresponding carriers
x_rec = x_rec+hat_zt(ii,:).*exp(j*2*pi*(RecSuppSorted(ii)-L0-1)*fp.*t_axis);
end
x_rec = real(x_rec);
figure(5)
x=1:length(x_rec);
y1=abs(fft(x_rec,length(x_rec)));
plot(x(1:length(x_rec)/2),y1(1:length(x_rec)/2));
title('重构传输信号频谱');