Created
October 8, 2021 23:31
-
-
Save gigamonkey/4a245ac509577681b2e7e99593c181a7 to your computer and use it in GitHub Desktop.
This is how I roll ...
This file contains 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
#!/usr/bin/env python | |
from math import cos, pi | |
def svg(width, height, s, fn): | |
zero = s(0) | |
points = " ".join(f"{x},{s(fn(x, width))}" for x in range(width)) | |
return f"""<?xml version="1.0" encoding="iso-8859-1"?> | |
<svg version="1.1" | |
xmlns="http://www.w3.org/2000/svg" | |
xmlns:xlink="http://www.w3.org/1999/xlink" | |
viewBox="0 0 100 50" | |
xml:space="preserve"> | |
<!-- <rect width="{width}" height="{height}" fill="pink"/> --> | |
<line x1="0" y1="{zero}" x2="{width}" y2="{zero}" stroke="#aaa" /> | |
<polyline points="{points}" fill="none" stroke="black" stroke-width="10px"/> | |
</svg> | |
""" | |
def scaler(d, r): | |
"Return a function that scales values in the domain d to the range r." | |
return lambda v: r[0] + (((v - d[0]) / (d[1] - d[0])) * (r[1] - r[0])) | |
def hanning(n, M): | |
# https://numpy.org/doc/stable/reference/generated/numpy.hanning.html#numpy.hanning | |
M -= 1 | |
return (0.5 - (0.5 * cos((2 * pi * n)/(M - 1)))) | |
def hamming(n, M): | |
# https://numpy.org/doc/stable/reference/generated/numpy.hamming.html#numpy.hamming | |
return (0.54 - (0.46 * cos((2 * pi * n)/(M - 1)))) | |
def blackman(n, M): | |
# https://numpy.org/doc/stable/reference/generated/numpy.blackman.html#numpy.blackman | |
return 0.42 - (0.5 * cos((2 * pi * n) / M) + (0.08 * cos((4 * pi * n) / M))) | |
if __name__ == "__main__": | |
width = 100 | |
height = 50 | |
s = scaler((-0.25, 1.0), ((height - 5), 5)) | |
with open("hanning.svg", mode="w") as f: | |
print("hanning") | |
print(svg(width, height, s, hanning), file=f) | |
with open("hamming.svg", mode="w") as f: | |
print("hamming") | |
print(svg(width, height, s, hamming), file=f) | |
with open("blackman.svg", mode="w") as f: | |
print("blackman") | |
print(svg(width, height, s, blackman), file=f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment