/ concept-collection / windowedFourierProjection
Sign in
concept-collection / windowedFourierProjection
77 lines · 1.5 KBBlameHistoryRaw
1function [ioffst, ibox, isradr,icnt] = assign(nboxes, xat, natoms)
2% ASSIGN Assigns sources and targets to boxes.
3%
4% INPUTS:
5% nboxes - Number of boxes
6% xat - Positions of sources
7% natoms - Number of sources
8%
9% OUTPUTS:
10% ioffst - Offsets for sources in each box
11% ibox - Addresses of the boxes containing each target
12% center - Centers of each box
13if nargin == 0, test_assign; return; end
15% xat =(xat - xat(1))./(xat(end) - xat(1));
17% Initialize variables
18h = 1 / nboxes;
19icnt = zeros(nboxes, 1);
20ibox = zeros(natoms, 1);
21isradr = zeros(natoms, 1);
23% Find box in which each source lies and increment counter
24for j = 1:natoms
25 ixh = floor(xat(j)/h);
26 if ixh >= nboxes
27 ixh = nboxes-1;
28 elseif ixh <= 0
29 ixh = 0;
30 end
31 iadr = ixh + 1;
32 icnt(iadr) = icnt(iadr) + 1;
33 ibox(j) = iadr;
34end
36% Compute ioffst array
37ioffst = zeros(nboxes, 1);
38ioffst(1) = 1;
39for j = 2:nboxes
40 ioffst(j) = ioffst(j-1) + icnt(j-1);
41end
43% Reset icnt for recording source locations in the original array
44icnt(:) = 0;
45for j = 1:natoms
46 iadr = ibox(j);
47 indx = ioffst(iadr) + icnt(iadr);
48 isradr(indx) = j;
49 icnt(iadr) = icnt(iadr) + 1;
50end
52end
54function test_assign
55as = -1; bs = 0;
56Ns = 10;
57s = linspace(as,bs,Ns);
58s_idx = randperm(Ns,Ns);
59s = s(s_idx);
61figure(1);
62plot(s,0,'*b');
64nboxes = 5;
66snew = (s - as)./(bs - as);
68[ioffst, ibox, isradr,icnt] = assign(nboxes, snew, Ns);
70plot(s,0,'b*',s(ioffst),0,'r|');
72box = 3
73start = ioffst(box)
74isradr(ioffst(box):(ioffst(box) + icnt(box) - 1))
76pause;
77end
moveopenescclose