Vous êtes sur la page 1sur 7

clc;

clearall;
closeall;
fismat=readfis(main.fis);
%PU STATUS
pu=input(Enter status of the PU (1=Present/0=Absent):);
% SCATTER PLOT
% Considering a geographic area with random number of CR terminals. Random
% count is generated by the following function
count=randi([25 50],1,1);
% Random x-y coordinates are generated for all CR terminals
X=randi([0 9],1,count)+0.01*randi([0 100],1,count);

Y=randi([0 9],1,count)+0.01*randi([0100],1,count);
% Scatter Plot showing location of all CR terminals
scatter(X,Y,100,g);
% The first CR terminal is selected as the central node and the distance
s
% from all otehr terminals to the central node are calculated and stored
in
% an array
for k=1:countdistanceunsort(k)=sqrt(((X(1)-X(k))^2)+((Y(1)-Y(k))^2));
end;
% The two closest neighbours are selected after sorting the distance array
distance=sort(distanceunsort);
for xx=1:3
forij=1:count
if ( distance(xx)== distanceunsort(ij))
x(xx)=X(ij);
y(xx)=Y(ij);
end
end
end
% Location of central node and neighbours in the scatter plot are
% displayed
for m=2:3
line([x(1),x(m)],[y(1),y(m)]);
pause(0.5);
end;
% Zoomed view of central node and its neighbours
figure;
scatter(x,y,100,g);
for m=2:3
line([x(1),x(m)],[y(1),y(m)]);
pause(1);
end;

% TRANSMITTER - PSK Modulator


x=randint(1,10);
%Random bit stream
xx=12;
yy=10;
b=2*x-1;
T = 0 . 0 1 e - 6 ; % B i t d u r a t i o n
E b = 1 0 * T / 2 ; % T h i s w i l l r e s u l t i n u n i t a m p l i t u d
e w a v e f o r m s
f c = 9 / T ; % C a r r i e r f r e q u e n c y
t=linspace(0,10,1000); % Discrete time sequence between 0 and 10 (1000 sample
s)
N = l e n g t h ( t ) ; % N u m b e r o f s a m p l e s
Nsb=N/length(x); % Number of samples per bit
dd=repmat(x,1,Nsb); % Replicate each bit Nsb times
bb=repmat(b,1,Nsb);
d w = d d ; % T r a n s p o s e t h e r o w s a n d
c o l u m n s
d w = d w ( : ) ; % C o n v e r t d w t o a c o l u m n v e c t o r ( c o l u
m b y c o l u m n ) a n d c o n v e r t
bw=bb;
b w = b w ( : ) ; % D a t a s e q u e n c e s a m p l e s
% Carrier waveform
w=sqrt(2*Eb/T)*cos(2*pi*fc*t);
b p s k _ w = b w . * w ; % P S K M o d u l a t e d W a v e
% Plots Bit Sequence to be transmitted
figure;
subplot(6,1,1);
plot(t,dw);
axis([0 yy -2 2])
title(Bit Sequence to be Transmitted);
% Plots transmitted signal
subplot(6,1,2);
plot(t,bpsk_w,.);
axis([0 yy -xx xx])
title(Transmitted Signal (After PSK Modulation));
% Loop to account for channel modelling for two neighbours

for ii=1:3
% CHANNEL MODELING
c = r a yl e i g h c h a n ( 1 / 1 0 0 0 , 1 0 0 ) ; % R a yl e i g h F a d i n g C h a n n e l M o d e l
if(pu==1)
y=filter(c,bpsk_w); % Convolution of transmitted signal with Rayleigh
Fa
else
y = 0 ; % N o t r a n s m i s s i o n ( A b s e n c e o
f P r i m a r y U s e r )
end;
% Random SNR generation between -25.00dB and 10.00dB
snr=randi([-25 9],1,1)+0.01*randi([0 100],1,1);
% Adding White Gaussian Noise with a defined value for SNR
q=awgn(y,snr);
% RECEIVER
% Band Pass Filter
ww=fdesign.bandpass(N,F3dB1,F3dB2,2,0.7e9,1.1e9,10e10); % Design BPF by specifyin
g
Hd=design(ww,butter); % Butterworth filter
z = f i l t e r ( H d , q ) ; % C o n v o l u t e d O u t p u t
% Power Detector
d a = f f t ( z ) ; % T a k i n g F F T o f r e c e i v e
d s i g n a l
v = d a . ^ 2 ; % S q u a r i n g a l l
t e r m s
% Taking average of all terms to obtain Power of received signal
power(ii)=20*log10(abs(mean(v)));
% Storing SNR of central node and neighbours
snnr(ii)=snr;
% Demodulator
we=z.*cos(2*pi*fc*t);
% Multiplying with carrier
%Low Pass Butterworth Filter - Integrator
wqq=fdesign.lowpass(N,Fc,4,1e9,10e10);
Hd2=design(wqq,butter);
zqq=filter(Hd2,we); %Convolution
end;

% Plots Received Signal


subplot(6,1,3);
plot(t,q);
title(Recieved Signal);
axis([0 yy -xx xx])
% Plots Band Pass Output
subplot(6,1,4);
plot(t,z);
title(Bandpass Output);
axis([0 yy -xx xx])
if(evalfis([power(1) snnr(1) power(2) power(3)],fismat)>=0.5)
%Regenerated Waveform
subplot(6,1,5)
plot(t,zqq);
title(Regenerated Waveform);
dec=real(zqq)./abs(real(zqq));
% Demodulated Signal
subplot(6,1,6)
plot(t,dec)
title(Demodulated Signal);
axis([0 yy -2 2])
% Obtaining Bit Stream through Sampling
deccount=1;
for k=100:100:1000
array(deccount)=(dec(k)+1)/2;
deccount=deccount+1;
end;
disp(Transmitted Bits : );disp(x);
disp(Receieved Bits : );disp(array);

end;
% FUZZY FILE
disp(CR1 Power (in dB): ); disp(power(1));
disp(CR1 SNR (in dB): ); disp(snnr(1));
disp(CR2 Power (in dB): ); disp(power(2));
disp(CR3 Power (in dB): ); disp(power(3));
% Single Node Decision
if (power(1)>30 &pu==1)
% sncount=sncount+1;
end;
% Fuzzy Decision
if(evalfis([power(1) snnr(1) power(2) power(3)],fismat)>=0.5)
disp(PU is Present);
else
disp(PU is Absent);
end;

Vous aimerez peut-être aussi