This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dt_channel.m
83 lines (73 loc) · 2.49 KB
/
dt_channel.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
function r=dt_channel(x)
global delayInSamples showPlots Fs
if ~isreal(x)
error(['This is a passband, not an equivalent '...
'baseband simulation. Channel input must be real!']);
end
SNRdB = 20; %desired signal to noise ratio in dB
%perform clipping and count the number of clippings:
maxAmplitude = 10;
minAmplitude = -10;
saturationIndices = x>maxAmplitude;
numOfSaturations = sum(saturationIndices);
if numOfSaturations > 0
warning(['Positive clips=' num2str(numOfSaturations)])
end
x(saturationIndices) = maxAmplitude;
saturationIndices = x<minAmplitude;
numOfSaturations = sum(saturationIndices);
if numOfSaturations > 0
warning(['Negative clips=' num2str(numOfSaturations)])
end
x(saturationIndices) = minAmplitude;
%filter
[B,A]=dt_getChannelTransferFunction();
%this channel has a passband from 0.3pi to 0.7pi, which
%corresponds to 0.3(Fs/2) to 0.7(Fs/2). For Fs=10000 Hz,
%this gives a band from 1500 to 3500, BW=2 kHz
%do not want to use filter, but conv:
h=impz(B,A,400); %approximate h[n] with 400 samples
z=conv(x,h); %note that z is longer than x
powerRxSignal = mean(abs(z).^2)
%Observing the plot, it was noticed that, at the band
%of interest, the group delay is approximately 7 samples
delayInSamples = delayInSamples + 7;
%add AWGN with desired power
SNR=10^(0.1*SNRdB); %SNR in linear scale
noisePower = powerRxSignal/SNR %noise power
%The code below is not necessary because it is not an
%equivalent complex baseband simulation:
%if useQAM == 1
% %generate complex noise, note that it is noisePower/2
% noise = sqrt(noisePower/2)*(randn(size(z))+...
% j*randn(size(z)));
%else
% noise = sqrt(noisePower)*randn(size(z));
%end
noise = sqrt(noisePower)*randn(size(z));
r = z + noise; %add noise
%estimate SNR
SNRactual=10*log10(mean(abs(z).^2)/mean(abs(noise).^2))
if showPlots
clf
subplot(221)
[H,f]=freqz(B,A,[],Fs); %show frequency response
plot(f,10*log10(abs(H)));
axis tight
title('Channel frequency response');
ylabel('|H(f)| (dB)')
subplot(222)
%writeEPS('dt_channel_group_delay','font12Only')
zplane(B,A) %show the filter is not minimum-phase
title('Channel poles and zeros');
subplot(223)
[g,f]=grpdelay(B,A,512,Fs); %group delay
plot(f,g);
xlabel('f (Hz)'); ylabel('Group delay (samples)');
title('Channel group delay');
axis tight
subplot(224)
pwelch(noise,[],[],[],Fs);
title('Noise (AWGN) PSD');
pause
end