This repository has been archived on 2021-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
ipcv/julia_notebook/dct.ipynb
Nguyễn Anh Khoa a43837539e init
2019-05-14 22:37:19 +07:00

151 lines
4.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using FFTW"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dct (generic function with 1 method)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function dct(f)\n",
" function a(u, N)\n",
" if u == 0\n",
" sqrt(1/N)\n",
" else\n",
" sqrt(2/N)\n",
" end\n",
" end\n",
"\n",
" if ndims(f) == 1\n",
" N = length(f)\n",
" [a(u, N) * sum([x * cos(pi * u * (2i - 1)/ (2N)) for (i, x) in enumerate(f)]) for u=0:N - 1]\n",
" elseif ndims(f) == 2\n",
" N, M = size(f)\n",
" [a(u, N) * a(v, M) * sum([f[x+1, y+1] * cos(pi * u * (2x + 1)/ (2N)) * cos(pi * v * (2y + 1) / (2M)) for x=0:N-1, y=0:M-1]) for u=0:N - 1, v=0:M-1]\n",
" else\n",
" error(\"What the hell man?\")\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8-element Array{Float64,1}:\n",
" 12.727922061357857 \n",
" -6.4423230227051365 \n",
" -1.3322676295501878e-15\n",
" -0.6734548009039396 \n",
" 0.0 \n",
" -0.20090290373599107 \n",
" -2.6645352591003757e-15\n",
" -0.05070232275964859 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8-element Array{Float64,1}:\n",
" 12.727922061357857 \n",
" -6.442323022705137 \n",
" 0.0 \n",
" -0.6734548009039407 \n",
" 0.0 \n",
" -0.20090290373599654 \n",
" 0.0 \n",
" -0.050702322759645924"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8×8 Array{Float64,2}:\n",
" 260.0 -18.2216 -3.51701e-14 … -4.89869e-14 -0.143408 \n",
" -145.773 -3.55271e-15 -1.77636e-15 3.01981e-14 1.02141e-14\n",
" -4.14504e-14 5.32907e-15 0.0 0.0 1.77636e-15\n",
" -15.2385 -3.55271e-15 3.55271e-15 -7.10543e-15 0.0 \n",
" 1.6329e-14 -1.77636e-15 3.55271e-15 -8.88178e-16 9.76996e-15\n",
" -4.54591 -5.32907e-15 0.0 … 0.0 5.9952e-15 \n",
" -5.33831e-14 1.15463e-14 -6.21725e-15 -4.44089e-16 -1.33227e-15\n",
" -1.14726 -2.66454e-15 -4.44089e-15 -2.22045e-15 2.66454e-15"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8×8 Array{Float64,2}:\n",
" 260.0 -18.2216 0.0 -1.90482 0.0 -0.568239 0.0 -0.143408\n",
" -145.773 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
" -15.2385 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
" -4.54591 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
" -1.14726 0.0 0.0 0.0 0.0 0.0 0.0 0.0 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(dct(1:8))\n",
"\n",
"display(FFTW.dct(1:8))\n",
"\n",
"display(dct([(i-1)*8 + j for i=1:8, j=1:8]))\n",
"\n",
"display(FFTW.dct([(i-1)*8 + j for i=1:8,j=1:8]))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.2.0-DEV",
"language": "julia",
"name": "julia-1.2"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}