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/dft_fft.ipynb
Nguyễn Anh Khoa a43837539e init
2019-05-14 22:37:19 +07:00

212 lines
5.6 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": 13,
"metadata": {},
"outputs": [],
"source": [
"using FFTW"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dft (generic function with 1 method)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function dft(f)\n",
" if ndims(f) == 1\n",
" M = length(f)\n",
" [sum([x * exp(-2im*pi*u * (idx - 1) / M ) for (idx, x) in enumerate(f)]) for u=0:M-1]\n",
" elseif ndims(f) == 2\n",
" M, N = size(f)\n",
" [sum([f[x+1, y+1] * exp(-2im*pi*(u*x/M + v*y/M)) for x=0:M-1, y=0:N-1]) for u=0:M-1, v=0:N-1]\n",
" else\n",
" error(\"Wrong dimension\")\n",
" end\n",
"end "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fft2d (generic function with 1 method)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function fft1d(array)\n",
" #= https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm\n",
" #\n",
" # Xk = Ek + e^(-2pi*i/N * k) * Ok\n",
" #\n",
" # Ek = Event DFT\n",
" # Ok = Odd DFT\n",
" #\n",
" =#\n",
" N = size(array)[1]\n",
" if N == 1\n",
" return array\n",
" elseif N % 2 != 0\n",
" error(\"Wrong dimension\")\n",
" end\n",
"\n",
" # people count from zero while julia counts from 1\n",
" # either way, it should be the same name\n",
" even = fft1d(array[1:2:end])\n",
" odd = fft1d(array[2:2:end])\n",
" w = exp.(-2im * pi / N * [i for i=0:N/2-1])\n",
" half1 = even .+ (odd .* w)\n",
" half2 = even .- (odd .* w)\n",
" vcat(half1,half2)\n",
"end\n",
"\n",
"function fft2d(matrix)\n",
" if ndims(matrix) != 2\n",
" error(\"I dont handle other than 2 dimension matrix\")\n",
" end\n",
" matrix_fft1 = hcat(fft1d.(eachrow(matrix))...)\n",
" hcat(fft1d.(eachrow(matrix_fft1))...)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8-element Array{Complex{Float64},1}:\n",
" 36.0 + 0.0im \n",
" -4.0 + 9.65685424949238im \n",
" -4.0 + 4.0im \n",
" -4.0 + 1.6568542494923806im\n",
" -4.0 + 0.0im \n",
" -4.0 - 1.6568542494923806im\n",
" -4.0 - 4.0im \n",
" -4.0 - 9.65685424949238im "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8-element Array{Complex{Float64},1}:\n",
" 36.0 - 0.0im \n",
" -4.0 + 9.65685424949238im \n",
" -4.0 + 4.0im \n",
" -4.0 + 1.6568542494923797im\n",
" -4.0 + 0.0im \n",
" -3.9999999999999996 - 1.6568542494923797im\n",
" -3.9999999999999996 - 4.0im \n",
" -3.9999999999999987 - 9.65685424949238im "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8-element Array{Complex{Float64},1}:\n",
" 36.0 - 0.0im \n",
" -4.000000000000003 + 9.65685424949238im \n",
" -4.000000000000002 + 3.9999999999999982im \n",
" -4.0 + 1.656854249492386im \n",
" -4.0 - 3.91886975727153e-15im\n",
" -4.0000000000000115 - 1.6568542494923912im \n",
" -4.000000000000018 - 4.000000000000005im \n",
" -3.9999999999999725 - 9.656854249492369im "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"2×8 Array{Complex{Float64},2}:\n",
" 72.0+0.0im -8.0+19.3137im -8.0+8.0im … -8.0-8.0im -8.0-19.3137im\n",
" 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"2×8 Array{Complex{Float64},2}:\n",
" 72.0-0.0im -8.0+19.3137im -8.0+8.0im … -8.0-8.0im -8.0-19.3137im\n",
" 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"2×8 Array{Complex{Float64},2}:\n",
" 72.0-0.0im -8.0-7.83774e-15im … -8.0-3.39081e-13im\n",
" 0.0-4.40873e-15im 0.0+4.89859e-16im 0.0-1.87804e-13im"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(fft(1:8))\n",
"display(fft1d(1:8))\n",
"display(dft(1:8))\n",
"\n",
"\n",
"display(fft([[1:8...] [1:8...]]'))\n",
"display(fft2d([[1:8...] [1:8...]]'))\n",
"display(dft([[1:8...] [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
}