├── newv.m ├── newmoo.m ├── README.md ├── newsigma.m ├── LICENSE └── GMM.m /newv.m: -------------------------------------------------------------------------------- 1 | function [ newv ] = newv( r,landa ) 2 | newv=sum(landa)./size(r,1); 3 | 4 | 5 | end 6 | 7 | -------------------------------------------------------------------------------- /newmoo.m: -------------------------------------------------------------------------------- 1 | function [ newmoo ] = newmoo(r,landa) 2 | for i=1:size(r,1) 3 | prod(i,:)=landa(i).*r(i,:); 4 | end 5 | newmoo=sum(prod)./sum(landa); 6 | end 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gaussian-Mixture-Model 2 | This is a part of MATLAB implementation of the paper "Machine Learning Techniques for Cooperative Spectrum Sensing in Cognitive Radio Networks" in which Gaussian Mixture Model clustering is employed. 3 | -------------------------------------------------------------------------------- /newsigma.m: -------------------------------------------------------------------------------- 1 | function [ newsigma ] = newsigma( landa,r,moo ) 2 | pro=zeros(size(r,2),size(r,2)); 3 | for i=1:size(r,1) 4 | pr=(landa(i)*(r(i,:)-moo)'*(r(i,:)-moo)); 5 | pro=pro+pr; 6 | end 7 | newsigma=pro./sum(landa); 8 | 9 | end 10 | 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Bahar Nikpour 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /GMM.m: -------------------------------------------------------------------------------- 1 | clc;clear all;close all 2 | T=100e-6; 3 | w=5e6; 4 | P=200; 5 | a=-4; 6 | samples=T.*w; 7 | etha=10.^(-17.4); 8 | Cn_x=[0 1].*1000; 9 | Cn_y=[0 0].*1000; 10 | plot(Cn_x,Cn_y,'o');axis([-500 1500 -1000 500]); 11 | Cm_x=[0.5 0].*1000; 12 | Cm_y=[0 -0.5].*1000; 13 | hold on;plot(Cm_x,Cm_y,'^'),grid on;legend('SU','PU') 14 | m=2;n=2; 15 | for i=1:m 16 | for j=1:n 17 | g(i,j)=sqrt(((Cm_x(i)-Cn_x(j)).^2)+((Cm_y(i)-Cn_y(j)).^2)); 18 | end 19 | end 20 | d=g.^a; 21 | prod=(2.*T.*P.*10.^-3)./etha; 22 | mean0=(ones(2,1).*2.*samples)'; 23 | mean1=(2.*samples)+(d(1,:).*prod); 24 | mean2=(2.*samples)+(d(2,:).*prod); 25 | mean3=(2.*samples)+((d(1,:)+d(2,:)).*prod); 26 | sigma0=diag(ones(2,1).*4.*samples); 27 | sigma1=diag((4.*samples)+(4.*(d(1,:).*prod))); 28 | sigma2=diag((4.*samples)+(4.*(d(2,:).*prod))); 29 | sigma3=diag((4.*samples)+(4.*((d(1,:)+d(2,:)).*prod))); 30 | r0=mvnrnd(mean0,sigma0,120); 31 | r1=mvnrnd(mean1,sigma1,120); 32 | r2=mvnrnd(mean2,sigma2,120); 33 | r3=mvnrnd(mean3,sigma3,120); 34 | r=[r0;r1; r2 ;r3]; 35 | label=[ones(120,1);ones(360,1).*2]; 36 | c1=r(label==1,:); 37 | c2=r(label==2,:);figure; 38 | plot(c1(:,1),c1(:,2),'c*'),hold on 39 | plot(c2(:,1),c2(:,2),'ms');title('original data');axis([800 1400 800 1400]) 40 | moo_g1=mean0;sigma_g1=sigma0;v1=0.25;v2=1-v1; 41 | moo_g2=[1250,1250];sigma_g2=[3000,0;0,3000]; 42 | for t=1:size(r,1) 43 | phi1(t)=(1./(((2.*pi).^(n/2)).*sqrt(det(sigma_g1)))).*exp(-0.5*(r(t,:)-moo_g1)*((sigma_g1)^(-1))... 44 | *(r(t,:)-moo_g1)'); 45 | end 46 | for iteration=1:100 47 | for t=1:size(r,1) 48 | phi2(t)=(1./(((2.*pi).^(n/2)).*sqrt(det(sigma_g2)))).*exp(-0.5*(r(t,:)-moo_g2)*((sigma_g2)^(-1))... 49 | *(r(t,:)-moo_g2)'); 50 | end 51 | %%%%%%%%E Step%%%%%%%%% 52 | landa1=(v1.*phi1)./((v1.*phi1)+(v2.*phi2)); 53 | landa2=(v2.*phi2)./((v1.*phi1)+(v2.*phi2)); 54 | %%%%%%%M Step%%%%%%%%%% 55 | moo_g2=newmoo(r,landa2); 56 | sigma_g2=newsigma(landa2,r,moo_g2); 57 | v1=newv(r,landa1); 58 | v2=newv(r,landa2); 59 | end 60 | ggg = gmdistribution([moo_g1;moo_g2], cat(3,sigma_g1,sigma_g2), [v2;v1]); 61 | figure; 62 | for x=1:size(r,1) 63 | pos(x,:)=posterior(ggg,r(x,:)); 64 | end 65 | for y=1:size(pos,1) 66 | if pos(y,1)>pos(y,2) 67 | ll(y)=1; 68 | else 69 | ll(y)=2; 70 | end 71 | end 72 | c1=r(ll==1,:); 73 | c2=r(ll==2,:); 74 | plot(c1(:,1),c1(:,2),'c*'),hold on 75 | plot(c2(:,1),c2(:,2),'ms');axis([800 1400 800 1400]);title('clustered data'); 76 | conmat=confusionmat(label,ll) 77 | error=((conmat(1,2)+conmat(2,1))./sum(sum(conmat))).*100 %%%%IN PERCENT --------------------------------------------------------------------------------