0% found this document useful (0 votes)
82 views

Cognitive Radio System: 1. Square-Root Raised Cosinefilter User Define Function

This document contains MATLAB code for a cognitive radio spectrum sensing system. It includes functions for transmitting a signal, applying modulation, adding channel noise, and using two spectrum sensing techniques: energy detection and matched filtering. The energy detection technique calculates the power spectral density and compares it to a threshold. The matched filtering technique correlates the received signal with possible carriers and compares correlation results to thresholds.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Cognitive Radio System: 1. Square-Root Raised Cosinefilter User Define Function

This document contains MATLAB code for a cognitive radio spectrum sensing system. It includes functions for transmitting a signal, applying modulation, adding channel noise, and using two spectrum sensing techniques: energy detection and matched filtering. The energy detection technique calculates the power spectral density and compares it to a threshold. The matched filtering technique correlates the received signal with possible carriers and compares correlation results to thresholds.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Appendix

Cognitive Radio System


1. Square-Root Raised Cosinefilter user define function:
1. function [response]=srrc(os_factor,roll_off)
2. %os_factor=oversampling factor
3. a=roll_off;
4. t=-4:1/os_factor:4; %Limiting the response to -4T to 4T
5. %This can be increased or decreased according to the requirement
6. p=zeros(1,length(t));
7. for i=1:1:length(t)
8. if t(i)==0
p(i)= (1-a)+4*a/pi;
9. elseif t(i)==1/(4*a) || t(i)==-1/(4*a)
p(i)=a/sqrt(2)*((1+2/pi)*sin(pi/(4*a))+(1-2/pi)*cos(pi/(4*a)));
else
p(i) = (sin(pi*t(i)*(1-a))+4*a*t(i).*cos(pi*t(i)*(1+a)))./(pi*t(i).*(1-(4*a*t(i)).^2));
end
10. end
11. end
12. response=p./sqrt(sum(p.^2)); %Normalization to unit energy
13. end

1. Transmitter.m MATLAB code:

1. close all;
2. clear all;
3. clc
4. disp(' ');
5. disp(' This program simulate a cognitive radio spectrum sensing
system: ');
6. disp(' ----------------------------------------------------------
------ ');
7. disp(' ');
8. disp(' - The system first received a signal from channel ');
9. disp(' ');

84
Appendix

10. disp(' - And then use spectrum sensing techniques to check


if primary user');
11. disp(' ');
12. disp(' allocate a channel or a channel is free.');
13. disp(' ');
14.
15.
16.
17. % % PARAMETERS %
18. f = 200; %operating frequency.
19. Fs = 20*f; %sampling frequency.
20. L=100; % Number of samples per symbol period.
21. Ts = 1/Fs; % Sampling period.
22. T = Ts:Ts:1/f;
23. alpha=0.5; % Roll-off factor for the (square-root)
raised cosine filters.
24. N=8*L; % N+1 is the length of the square-root
raised-cosine filter.
25. sigma_v=0; % Standard deviation of channel noise.
26. h=1; % Channel impulse response.
27.
28.
29. % %SOURCE: Take input data from user for transmission %
30. pt_dt = input('Data you want to send:','s');
31. R = isempty(pt_dt);
32. if R == 1
33. display('primary user is absent')
34. RR=1;
35. else
36. pt_dt = pt_dt;
37.
38. display(pt_dt);
39. % [255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255]
40. RR=double(pt_dt)
41. end
42. bb = 1;
43. Rp = dec2bin(RR,8);
44. [TA, TC] = size(Rp);
45. forll = 1:1:TA
46. forlg = 1:1:TC
47. msg(bb) = Rp(ll,lg);
48. bb = bb + 1;
49. end
50. end
51. rt = 1;
52. ht = 1;
53. forls = 1:1:TA
54. forll = 1:2:(TC)
55. Inp_msg(rt,(ht:ht+1)) = Rp(ls,(ll:ll+1));

85
Appendix

56. rt = rt + 1;
57. end
58. end
59. % % Transmit Filter %
60. % pT=sr_cos_p(N,L,alpha); % Transmit filter:
61.
62. overSampling_Factor=8;
63.
64. pT = srrc(overSampling_Factor,alpha); % impulse response of
SRRC filter
65.
66. Input_bit_os=upsample(RR,8); %oversampling
67. % Input=double(Input_bit_os); %conv not work with binary
convert it to double
68.
69. xT=conv(Input_bit_os,pT); %Transmit signal (the user signal
send it pt_dt)
70. % xT=conv(Input_bit_os,pT); % Transmit signal
71.
72. % % Modulation %
73. display('Select Type of Modulation');
74. display('1. BPSK'); display('2. QPSK');
75. Mod_Type = input('Plz Enter the Type of Modulation:','s');
76.
77. snr = input('Plz Enter the SNR');
78.
79. Carrier = [];
80. % % BPSK Modulation %
81. if (Mod_Type=='1')
82. m=582370;
83. display('Binary PSK');
84. for ii = 1:1:length(T)
85. car1(ii) = sin((2*pi*f*T(ii))); %CARRIER
TO BE TRANSMITTED
86. end
87. for ii = 1:1:length(xT)
88. ifxT(ii) == '0'
89. car = -1*car1;
90. else
91. car = 1*car1;
92. end
93. Carrier = [Carrier car];
94. end
95. % % QPSK Modulation %
96. elseif(Mod_Type=='2')
97. m=10907;
98. display('Quadrature PSK');
99. for ii = 1:1:length(T)
100. car1(ii) = sin((2*pi*f*T(ii))+360); %CARRIER TO
BE TRANSMITTED

86
Appendix

101. car2(ii) = sin((2*pi*f*T(ii))+90); %CARRIER TO


BE TRANSMITTED
102. car3(ii) = sin((2*pi*f*T(ii))+180); %CARRIER TO
BE TRANSMITTED
103. car4(ii) = sin((2*pi*f*T(ii))+270); %CARRIER TO
BE TRANSMITTED
104. end
105. for ii = 1:1:length(Inp_msg)
106. ifInp_msg(ii) == '00'
107. car = car1;
108. elseifInp_msg(ii) == '01'
109.
110. car = car2;
111. elseifInp_msg(ii) == '10'
112. car = car3;
113. elseifInp_msg(ii) == '11'
114. car = car4;
115. end
116. end
117. end
118. end
119. Carrier = [Carrier car];
120. end
121. end% end of if
122. end%end of else if
123.
124. % % CHANNEL %
125.
126. xR1=conv(xT,Carrier);
127. % xR=(xR1)+sigma_v*randn(size(xR1)); % Received signal ;;
128. xR=awgn(xR1,snr);
129.
130.
131.
132. % % % sensing type %%
133. % display('Select Type of spectrum sensing techniques:');
134. % display('1. Energy detection algorithm');
135. % display('2. Matched filter algorithm');
136. % tech_type = input('Plz it ','s');
137.

2. Energy detection.m MATLAB code:

1. disp('1. Energy detection sensing technique');


2. disp(' ');
3. Pxx = periodogram(xR);
4. Hpsd = dspdata.psd(Pxx,'Fs',Fs);
5. maxHpsd= 10*log10(max(Pxx))

87
Appendix

6. HpsdFreq = Hpsd.Frequencies;
7. Hpsddata = Hpsd.data;
8.
9.
10. figure
11. hold on
12. plot(Hpsd)
13.
14. %%%%%%%%% threshold line %%%%%%%%%%
15. for ii=1:1:11
16. xmax(ii)=10*log10(m);
17. end
18. c=0:0.2:2;
19.
20. plot(c,xmax,'k:')
21. axis([0 2 -80 80]);
22. legend('Data PSD','Threshold');
23. grid off
24. hold off
25.
26.
27.
28. if (max(Hpsddata)> m)
29. disp('primary user is present');
30. else
31. disp('primary user is absent');
32. end
33. figure

3. Matched filter.m MATLAB code:

1. for ii = 1:1:length(T)
2. car1(ii) = sin((2*pi*f*T(ii))+360); %CARRIER TO BE
TRANSMITTED
3. car2(ii) = sin((2*pi*f*T(ii))+90); %CARRIER TO BE
TRANSMITTED
4. car3(ii) = sin((2*pi*f*T(ii))+180); %CARRIER TO BE
TRANSMITTED
5. car4(ii) = sin((2*pi*f*T(ii))+270); %CARRIER TO BE
TRANSMITTED
6. end
7.
8. res1= xcorr(xR(1:20),car1)* 10^14;
9. res2= xcorr(xR(1:20),car2)* 10^14;
10. res3= xcorr(xR(1:20),car3)* 10^14;
11. res4= xcorr(xR(1:20),car4)* 10^14;
12.
13. r1=mean(res1)*10000;
14. r2=mean(res2)*10000;

88
Appendix

15. r3=mean(res3)*10000;
16. r4=mean(res4)*10000;
17. disp(' ');
18. disp('2. Matched filter sensing technique');
19. disp(' ');
20. if ((r1>1690)&& (r2<-560) && (r3>1250) && (r4<-2300))
21. %Primary user is present
22. display ('primary user is present');
23. else%Primary user is absent
24. display ('primary user is absent');
25. end
26. disp(' ');
27.
28. r1
29. r2
30. r3
31. r4
32. subplot(2,1,1)
33. plot(res1+res2+res3+res4)
34. legend('Received Signal')
35. axis([0 40 -10^15 10^15]);
36.
37. xlabel('Time (sec)')
38. ylabel('Amplitude')
39.
40.
41. %%_________________________________________________________
_________________________
42.
43.
44.
45. % %threshold SOURCE: Take input data from user for
transmission %
46. % pt_dt = input('Data you want to send:','s');
47. % R = isempty(pt_dt);
48. RR1=[33 33];
49. bb = 1;
50. Rp1 = dec2bin(RR1,8);
51. [TA1, TC1] = size(Rp1);
52. forll = 1:1:TA1
53. forlg = 1:1:TC
54. msg1(bb) = Rp1(ll,lg);
55. bb = bb + 1;
56. end
57. end
58. rt = 1;
59. ht = 1;
60. forls = 1:1:TA1
61. forll = 1:2:(TC1)

89
Appendix

62. Inp_msg1(rt,(ht:ht+1)) = Rp1(ls,(ll:ll+1));


63. rt = rt + 1;
64. end
65. end
66. % % Transmit Filter %
67. % pT=sr_cos_p(N,L,alpha); % Transmit filter:
68.
69. overSampling_Factor1=8;
70.
71. pT1 = srrc(overSampling_Factor1,alpha); % impulse response
of SRRC filter
72.
73. Input_bit_os1=upsample(RR1,8); %oversampling
74. % Input=double(Input_bit_os); %conv not work with binary
convert it to double
75.
76. xT1=conv(Input_bit_os1,pT1); %Transmit signal (the user
signal send it pt_dt)
77. % xT=conv(Input_bit_os,pT); % Transmit signal
78.
79. % % Modulation %
80.
81.
82. for ii = 1:1:length(T)
83. car1(ii) = sin((2*pi*f*T(ii)));
%CARRIER TO BE TRANSMITTED
84. end
85. for ii = 1:1:length(xT)
86. ifxT(ii) == '0'
87. car = -1*car1;
88. else
89. car = 1*car1;
90. end
91. Carrier1 = [Carrier car];
92. end
93.
94.
95. % % CHANNEL %
96. xR2=conv(xT1,Carrier1);
97. xR3=xR2+sigma_v*randn(size(xR2)); % Received signal ;;
98.
99.
100. %%%%%%%%%%%%%%%%%%% Matched Filter algorithm
%%%%%%%%%%%%%%%%%%%%%%%%
101.
102.
103. for ii = 1:1:length(T)
104. car1(ii) = sin((2*pi*f*T(ii))+360); %CARRIER TO BE
TRANSMITTED

90
Appendix

105. car2(ii) = sin((2*pi*f*T(ii))+90); %CARRIER TO BE


TRANSMITTED
106. car3(ii) = sin((2*pi*f*T(ii))+180); %CARRIER TO BE
TRANSMITTED
107. car4(ii) = sin((2*pi*f*T(ii))+270); %CARRIER TO BE
TRANSMITTED
108. end
109.
110. res5= xcorr(xR3(1:20),car1)* 10^14;
111. res6= xcorr(xR3(1:20),car2)* 10^14;
112. res7= xcorr(xR3(1:20),car3)* 10^14;
113. res8= xcorr(xR3(1:20),car4)* 10^14;
114.
115. subplot(2,1,2)
116.
117. plot((res5+res6+res7+res8))
118. legend('Referance signal')
119. axis([0 40 -10^15 10^15]);
120.
121. xlabel('Time (sec)')
122. ylabel('Amplitude')

91

You might also like