0% found this document useful (0 votes)
50 views3 pages

For For If Else End

This document contains code for particle swarm optimization to minimize electromagnetic emissions. It initializes a swarm of particles with random positions and velocities. It then iterates to evaluate each particle's fitness, update velocities and positions, and track the best global position. The optimized frequencies and emissions are then plotted against a mask to check compliance.

Uploaded by

hikol
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

For For If Else End

This document contains code for particle swarm optimization to minimize electromagnetic emissions. It initializes a swarm of particles with random positions and velocities. It then iterates to evaluate each particle's fitness, update velocities and positions, and track the best global position. The optimized frequencies and emissions are then plotted against a mask to check compliance.

Uploaded by

hikol
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 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]);

You might also like