Вы находитесь на странице: 1из 3

clear

clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Tmin=-4e-9;
Tmax=4e-9;
smp = 1024;
dt = (Tmax-Tmin) / smp;
fs = 1/dt;
Ts=1/fs;
frequencysmoothingfactor = 8;
N = frequencysmoothingfactor * smp;
df = 1 / (N * dt);
%y= 0.314e-9;
positivefrequency=linspace(0,(fs/2),N/2);
t=linspace(Tmin,Tmax,smp);
emissionmask = cp0703_generate_mask(N, fs);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
iterations = 30;
inertia = 1.0;
correction_factor = 2.0;
swarm_size = 14;
%rand('state',sum(10*clock));
val=[];
% ---- initial swarm position ----index = 1;
for i = 1 : 7
for j = 1 : 2
if rand < (0.5)
swarm(index, 1, 1) = rand*.01
else
swarm(index, 1, 1)= -rand*.01
end
swarm(index, 1, 2) = rand*1e-9;
index = index + 1;
end
end

swarm(:, 4, 1) = -1000;
swarm(:, 2, :) = 0;

% best value so far


% initial velocity

%% Iterations
for iter = 1 : iterations
%-- evaluating position & quality --for i = 1 : swarm_size
swarm(i, 1, 1) = swarm(i, 1, 1) + swarm(i, 2, 1)/1.3;
swarm(i, 1, 2) = swarm(i, 1, 2) + swarm(i, 2, 2)/1.3;
x = swarm(i, 1, 1);
y = swarm(i, 1, 2);

%update x position
%update y position

deriv= inputwaveforms(t,y);
norderiv5=deriv/max(abs(deriv));

combo = x * norderiv5;
EX=fft(combo,N);
EX=EX/N;
E = fftshift(abs(EX).^2/(df^2));
Ess = 2.*E((N/2+1):N);
PSD = 10 * log10 ((1/Ts) * Ess / 377) + 90;
if all(PSD < emissionmask);
power = sum(1/Ts .* Ess.*df / 377);
else
power=0;
end
if power > swarm(i, 4, 1);
% if new position is better
swarm(i, 3, 1) = swarm(i, 1, 1) % update best x,
swarm(i, 3, 2) = swarm(i, 1, 2) % best y postions
swarm(i, 4, 1) = power
% and best value
end
end
[temp, gbest] = min(swarm(:, 4, 1))

% global best position

%--- updating velocity vectors


for i = 1 : swarm_size
swarm(i, 2, 1) = rand*inertia*swarm(i, 2, 1) + correction_factor*rand*(swarm(i, 3, 1) swarm(i, 1, 1)) + correction_factor*rand*(swarm(gbest, 3, 1) - swarm(i, 1, 1)); %x velocity
component
swarm(i, 2, 2) = rand*inertia*swarm(i, 2, 2) + correction_factor*rand*(swarm(i, 3, 2) swarm(i, 1, 2)) + correction_factor*rand*(swarm(gbest, 3, 2) - swarm(i, 1, 2)); %y velocity
component
end

%% Plotting the swarm


% clf
% plot(swarm(:, 1, 1), swarm(:, 1, 2), 'x') % drawing swarm movements
% axis([0 1e-1 0 1e-10]);
%pause(.2)
end
figure()
plot(positivefrequency/1e6,...
emissionmask,'r','Linewidth',[1]);
hold on;
plot(positivefrequency/1e6, PSD);
AX=gca;
set(AX,'FontSize',12);
T=title('Random combination');
set(T,'FontSize',14);
X=xlabel('Frequency [MHz]');
set(X,'FontSize',14);
Y=ylabel('PSD [dBm/MHz]');
set(Y,'FontSize',14);
axis([0 12e3 -400 0]);

Вам также может понравиться