Created
December 23, 2023 03:47
-
-
Save aduffey/d098132676fb566a0d766aefb4199efb to your computer and use it in GitHub Desktop.
CRT simulation work
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
from imageio.v3 import imwrite | |
import numpy as np | |
def cos_isotropic_spot(x, y, s): | |
"""Takes scalar x, scalar y, and vector s""" | |
return sqrt(s) * (0.5 + 0.5 * np.cos(np.pi * np.clip(np.divide(np.sqrt(x**2 + y**2), sqrt(s), out=np.zeros_like(s), where=horiz!=0), -1, 1))) | |
def line(x, y, s): | |
"""Takes scalar x, scalar y, and vector s""" | |
np.trapz(cos_isotropic_spot(x, y, s), | |
def linear_to_srgb(img): | |
"""Linear float to sRGB uint8""" | |
out = np.where(img < 0.0031308, img * 12.92, 1.055 * (np.power(img, (1.0 / 2.4))) - 0.055) | |
out = (img * 255).astype(np.uint8) | |
return out | |
def main(): | |
xaxis = np.linspace(0, 320, 3200) | |
yaxis = np.linspace(-2, 2, 40) | |
taxis = np.linspace(0, 320, 3200) |
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f6dacace", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"var('x', 'xi')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3adb7b8a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"hann = plot(integral((0.5 + 0.5 * cos(pi * x)) * exp(-2 * pi * I * x * xi), x, -1, 1), xmin=0, xmax=3, color=rainbow(4)[0], legend_label=\"hann\", aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "09820d4f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"rect = plot(integral((1) * exp(-2 * pi * I * x * xi), x, -0.5, 0.5), xmin=0, xmax=3, color=rainbow(4)[1], legend_label=\"rect\", aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f533aa94", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cosine = plot(integral((1/1.27324 * cos(0.5 * pi * x)) * exp(-2 * pi * I * x * xi), x, -1, 1), xmin=0, xmax=3, color=rainbow(4)[2], legend_label=\"cos\", aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1665fb55", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"triangle = plot(integral((x + 1) * exp(-2 * pi * I * x * xi), x, -1, 0) + integral((1 - x) * exp(-2 * pi * I * x * xi), x, 0, 1), xmin=0, xmax=3, color=rainbow(4)[3], legend_label=\"triangle\", aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "79684acb", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(hann + rect + cosine + triangle)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1a5ec61a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(integral(0.93 * (0.54 + 0.46 * cos(pi * x)) * exp(-2 * pi * I * x * xi), x, -1, 1), xmin=0, xmax=3, color=rainbow(4)[0], legend_label=\"hamming\", aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "013de48c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sd = 0.4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c04243ed", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(integral((1 / (sd * sqrt(2 * pi))) * exp(-0.5* (x / sd)**2) * exp(-2 * pi * I * x * xi), x, -2, 2), xmin=0, xmax=3, color=rainbow(4)[0], legend_label=\"gaussian\", aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9b9e0c2a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(integral(((sin(pi * x) * sin(pi * x)) / (pi * x)**2) * exp(-2 * pi * I * x * xi), x, -1, 1), xmin=0, xmax=3, aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "a861036f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(integral(((2 * sin(pi * x) * sin(pi * x / 2)) / (pi * x)**2) * exp(-2 * pi * I * x * xi), x, -2, 2), xmin=0, xmax=3, aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9f88bc2a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(((sin(pi * x) * sin(pi * x)) / (pi * x)**2), xmin=-1, xmax=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "bd614483", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot((2 * sin(pi * x) * sin(pi * x / 2)) / (pi * x)**2, xmin=-2, xmax=2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "6788f0b8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"assume(xi>0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e290da40", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(integral((sin(pi * x) / (pi * x)) * exp(-2 * pi * I * x * xi), x, -999999, 999999), xmin=0, xmax=3, aspect_ratio=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "6bb28219", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "SageMath 9.8", | |
"language": "sage", | |
"name": "sagemath" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
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
from imageio.v3 import imwrite | |
import numpy as np | |
def cos_sep_spot(x, y): | |
return (0.5 + 0.5 * np.cos(np.pi * np.clip(x, -1, 1))) * (0.5 + 0.5 * np.cos(np.pi * np.clip(y, -1, 1))) | |
def cos_sep_2_spot(x, y): | |
horiz = 0.5 + 0.5 * np.cos(np.pi * np.clip(x, -1, 1)) | |
return horiz * (0.5 + 0.5 * np.cos(np.pi * np.clip(np.divide(y, np.power(horiz, 1/5), out=np.zeros((800, 800)), where=horiz!=0), -1, 1))) | |
def cos_isotropic_spot(x, y): | |
return (0.5 + 0.5 * np.cos(np.pi * np.clip(np.sqrt(x**2 + y**2), -1, 1))) | |
def gauss_spot(x, y): | |
c = 1 / (2 * np.sqrt(2 * np.log(2))) | |
#return 1 / (np.sqrt(2 * np.pi) * c) * np.exp(-x**2 / (2 * c**2)) * np.exp(-y**2 / (2 * c**2)) | |
return np.exp(-x**2 / (2 * c**2)) * np.exp(-y**2 / (2 * c**2)) | |
def linear_to_srgb(img): | |
"""Linear float to sRGB uint8""" | |
out = np.where(img < 0.0031308, img * 12.92, 1.055 * (np.power(img, (1.0 / 2.4))) - 0.055) | |
out = (img * 255).astype(np.uint8) | |
return out | |
def main(): | |
xaxis = np.linspace(-2, 2, 800) | |
yaxis = np.linspace(-2, 2, 800) | |
gauss_result = gauss_spot(xaxis[:,None], yaxis[None,:]) | |
print(gauss_result[400, 400], gauss_result[500, 400]) | |
imwrite("gauss_spot.png", linear_to_srgb(gauss_result)) | |
cos_sep_result = cos_sep_spot(xaxis[:,None], yaxis[None,:]) | |
print(cos_sep_result[400, 400], cos_sep_result[500, 400]) | |
imwrite("cos_sep_spot.png", linear_to_srgb(cos_sep_result)) | |
cos_sep_2_result = cos_sep_2_spot(xaxis[:,None], yaxis[None,:]) | |
print(cos_sep_2_result[400, 400], cos_sep_2_result[500, 400]) | |
imwrite("cos_sep_2_spot.png", linear_to_srgb(cos_sep_2_result)) | |
cos_isotropic_result = cos_isotropic_spot(xaxis[:,None], yaxis[None,:]) | |
print(cos_isotropic_result[400, 400], cos_isotropic_result[500, 400]) | |
imwrite("cos_isotropic_spot.png", linear_to_srgb(cos_isotropic_result)) | |
diff = np.abs(cos_isotropic_result - cos_sep_2_result) * 10 | |
imwrite("cos_diff.png", linear_to_srgb(diff)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment