Skip to content

Instantly share code, notes, and snippets.

@cfelton
Last active Dec 7, 2015
Embed
What would you like to do?
code snips for http://www.fpgarelated.com/showarticle/578.php, see link for description.
pix_t median(pix_t window[N])
{
pix_t t[N], z[N];
int ii, k, stage;
// copy locally
for(ii=0; ii<N; ii++) z[ii] = window[ii];
for(stage=1; stage<=N; stage++){
k = (stage%2==1) ? 0 : 1;
for(ii=k; ii<N-1; ii++){
t[ii] = MIN(z[ii], z[ii+1]);
t[ii+1] = MAX(z[ii], z[ii+1]);
z[ii] = t[ii];
z[ii+1] = t[ii+1];
}
}
return z[N/2];
}
def median(x):
N = len(x)
def compare_stage(z, stage, N):
t, k = copy(z), 0 if (stage%2) else 1
for ii in range(k, N-1, 2):
t[ii] = min(z[ii], z[ii+1])
t[ii+1] = max(z[ii], z[ii+1])
return t
z = x
for stage in range(N):
z = compare_stage(z, stage, N)
return z[N//2], z
def compare(clock, reset, x, z, stage=0):
N, K = len(z), 0 if stage%2 else 1
B = 0 if K == 1 else N-1
@always_seq(clock.posedge, reset=reset)
def beh():
z[B].next = x[B] # pass-thru
for ii in range(K, N-1, 2):
z[ii].next = x[ii] if x[ii] < x[ii+1] else x[ii+1]
z[ii+1].next = x[ii] if x[ii] > x[ii+1] else x[ii+1]
return beh
def median_distributed(clock, reset, wi, wo, med):
N, st = len(wi), wi[0]
z = [wi] + [[Signal(st.val) for _ in range(N)]
for stage in range(N-1)] + [wo]
cmp_inst = [compare(clock, reset, z[ii], z[ii+1], ii)
for ii in range(N)]
mid = N//2
@always_comb
def beh_assign():
med.next = wo[mid]
return cmp_inst, beh_assign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment