Trying to Generate DCT Matrix of Size 4x4, but it seems im doing something wrong as the cos() function returns 0

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Trying to Generate DCT Matrix of Size 4x4, but it seems im doing something wrong as the cos() function returns 0

10 Jul 2020, 05:20

hi im trying to make a dct matrix

wikipedia: https://en.wikipedia.org/wiki/Discrete_cosine_transform
video=>https://greencoin.life/how-to/calculate/dct-coefficients-with-example/CAn-FzQkGTA <= not the best video but hey!

i've split up the exp to try and debug it and for now it seems that the issue is somewhere before the Cos() function, no matter what value it passes to cos() it returns 0

this is the code i have for now

Code: Select all

; Generate DCT Matrix of Size NxN
pi := 3.141592653589793
; c = (c(k,n)) ; k=Rows , n=columns

; K := 4 ; rows
; N := 4 ; cols
; if (k=0)
;	 C := 1/Sqrt(N) 
; if (k>=1)
;	 C := Sqrt( 2/N ) * cos(((pi*((2*n)+1))*K) / ( 2*N ) )

K_Rows := 4
N_cols := 4
2d := []

loop % K_Rows
{
	k:=A_index-1
	loop % N_cols 
	{
		n:=A_index-1
		if (k=0)
		{
			2d[k,n] := 1/Sqrt(N_cols) 
		}
		else if (k>=1)
		{
			a:= 2/N_cols
			;msgbox % "a=" a
			b:= 2*n
			;msgbox % "b=" b
			c:= b+1
			;msgbox % "c=" c
			d:= c*pi
			;msgbox % "d=" d
			e:= d * K_Rows
			;msgbox % "e=" e
			f:= 2*N_cols
			;msgbox % "f=" f
			g:= Sqrt( a )
			;msgbox % "g=" g
			h := e / f
			;msgbox % "h=" h
			i:= cos( h )
			msgbox % "i=" i
			value := g * i
			;msgbox % "value=" value
			2d[k,n] := value
			; 2d[k,n] := Sqrt( 2/N_cols ) * cos( ( ( pi * ( (2*n) +1 ) ) * K_Rows ) / ( 2*N_cols ) )
		}
	}
}

stringArray := "["
for key,val in 2d
{
	for i, v in val
	{
		stringArray .= v ","
	}
	StringTrimRight, stringArray,stringArray, 1
	stringArray .= "`n"
}
StringTrimRight, stringArray,stringArray, 1
stringArray .= "]"
msgbox % stringArray
but when trying it, every row after 1 returns 0

Im hoping to use the discrete cosine transformation to extract the frequency of an image

hopefully one of you can help spot my mistake
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trying to Generate DCT Matrix of Size 4x4, but it seems im doing something wrong as the cos() function returns 0

13 Jul 2020, 04:20

Looks like not many mathematicians in here ... :think:
Where's Laszlo when you need'm ?! :( Maybe @lexikos can shed some light on this??
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Trying to Generate DCT Matrix of Size 4x4, but it seems im doing something wrong as the cos() function returns 0

14 Jul 2020, 02:03

Hi Blackholyman,
i have tried the dct from this https://pdfs.semanticscholar.org/98ce/2656aad1746e1250f94d5f71527e79ac7996.pdf
At first I couldn't understand it. I think there is a mistake in the formulas, but the author has calculated correctly. Especially the factor a(n) (ck) was used wrong in the examples. But in the definition 4.6.1 it is probably correct.
I could reproduce the example with the numerical values with the following:

Code: Select all

loop % K_Rows
{
	k:=A_index-1
	loop % N_cols 
	{
		n:=A_index-1
		if (k=0)
            2d[k,n] :=    round( 	1/Sqrt(N_cols) * cos( ((2*n +1 ) * k * pi   / ( 2*N_cols ))     ),2)
		else 
			2d[k,n] :=    round( 	Sqrt(2/N_cols) * cos( ((2*n +1 ) * k * pi   / ( 2*N_cols ))     ),2 )
	}
	
	/*
[0.50	0.50	0.50	0.50
0.65	0.27	-0.27	-0.65
0.50	-0.50	-0.50	0.50
0.27	-0.65	0.65	-0.27]
	*/
}
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Trying to Generate DCT Matrix of Size 4x4, but it seems im doing something wrong as the cos() function returns 0

17 Jul 2020, 03:24

I just found a report that says that the discrete cosine transformation can be used to unmask deepfakes.
Unfortunately the report is here in German:
https://www.spektrum.de/news/mathematische-analyse-soll-alle-deep-fakes-enttarnen/1752250

And here is the original source:
https://proceedings.icml.cc/static/paper_files/icml/2020/1539-Paper.pdf

Maybe this will be interesting for you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], gekunfei, PAT06, Rohwedder and 105 guests