Last active
December 7, 2015 15:01
-
-
Save cfelton/dc668eb28913a0e0d18b to your computer and use it in GitHub Desktop.
code snips for http://www.fpgarelated.com/showarticle/578.php, see link for description.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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