DCT変換

 投稿者:しばっち  投稿日:2010年10月30日(土)20時09分15秒
  乱数により生成した数列を変換、更に逆変換して
元の数列に戻ることを確認する。
!'DCT変換(テストルーチン)

DECLARE EXTERNAL  FUNCTION C
PUBLIC NUMERIC N
LET  N = 8
OPTION BASE 0
DIM X(N), Y(N), S(N, N)
RANDOMIZE
PRINT "初期値"
FOR I = 0 TO N - 1
   LET  X(I) = INT(RND * 256)
   PRINT USING "####":X(I);
NEXT I
PRINT
CALL DCT2(X, N, Y) !'離散COSINE変換
PRINT "DCT2"
FOR I = 0 TO N - 1
   PRINT Y(I);
NEXT I
PRINT
PRINT "DCT3"
CALL DCT3(Y, N, X) !'離散COSINE逆変換
FOR I = 0 TO N - 1
   PRINT USING "####":X(I);
NEXT I
PRINT
PRINT "初期値"
FOR I = 0 TO N - 1
   LET  X(I) = INT(RND * 256)
   PRINT USING "####":X(I);
NEXT I
PRINT
CALL DCT4(X, N, Y) !'DCT4変換
PRINT "DCT4"
FOR I = 0 TO N - 1
   PRINT Y(I);
NEXT I
PRINT
CALL DCT4(Y, N, X) !'DCT4逆変換
PRINT "DCT4"
FOR I = 0 TO N - 1
   PRINT USING "####":INT(X(I) + .5);
NEXT I
PRINT
PRINT "初期値"
FOR I = 0 TO N
   LET  X(I) = INT(RND * 256)
   PRINT USING "####":X(I);
NEXT I
PRINT
CALL DCT1(X, N, Y) !'DCT1変換
PRINT "DCT1"
FOR I = 0 TO N
   PRINT Y(I);
NEXT I
PRINT
CALL DCT1(Y, N, X) !'DCT1変換
PRINT "DCT1"
FOR I = 0 TO N
   PRINT USING "####":INT(X(I) + .5);
NEXT I
PRINT
PRINT "初期値"
FOR I = 0 TO N - 1
   FOR J = 0 TO N - 1
      LET  S(I, J) = INT(RND * 256)
      PRINT USING " ####":S(I, J);
   NEXT J
   PRINT
NEXT I
PRINT
PRINT "2次元DCT"
CALL DCT2D(S, N)
FOR I = 0 TO N - 1
   FOR J = 0 TO N - 1
      PRINT S(I, J);
   NEXT J
   PRINT
NEXT I
PRINT
!'フィルター処理(ここを実行すると元の数列には戻らない)
!'FOR I = 0 TO N-1
!'   FOR J = 0 TO N-1
!'      READ M
!'      LET S(I, J) = S(I, J) * M
!'   NEXT J
!'NEXT I
DATA 1,1,1,1,1,1,1,1  !'LPF(8*8)(ローパスフィルター)
DATA 1,1,1,1,1,1,1,0
DATA 1,1,1,1,1,1,1,0
DATA 1,1,1,1,1,1,1,0
DATA 1,1,1,1,1,1,1,0
DATA 1,1,1,1,1,1,0,0
DATA 1,1,1,1,1,0,0,0
DATA 1,0,0,0,0,0,0,0
PRINT "2次元逆DCT"
CALL IDCT2D(S, N)
FOR I = 0 TO N - 1
   FOR J = 0 TO N - 1
      PRINT USING " ####":INT(S(I, J)+.01);
   NEXT J
   PRINT
NEXT I
PRINT
END

EXTERNAL  FUNCTION C (X)
IF X = 0 OR X = N THEN LET  C = SQR(.5) ELSE LET  C = 1
END FUNCTION

EXTERNAL  SUB DCT1 (A(), N, B())
FOR K = 0 TO N
   LET  S = 0
   FOR I = 0 TO N
      LET  S = S + C(I) * A(I) * COS(I * K * PI / N)
   NEXT I
   LET  B(K) = S * SQR(2 / N) * C(K)
NEXT K
END SUB

EXTERNAL  SUB DCT2 (A(), N, B())
FOR I = 0 TO N - 1
   LET  S = 0
   FOR K = 0 TO N - 1
      LET  S = S + A(K) * COS((2 * K + 1) * I * PI / 2 / N)
   NEXT K
   LET  B(I) = S * SQR(2 / N) * C(I)
NEXT I
END SUB

EXTERNAL  SUB DCT3 (A(), N, B())
FOR I = 0 TO N - 1
   LET  S = 0
   FOR K = 0 TO N - 1
      LET  S = S + C(K) * A(K) * COS((2 * I + 1) * K * PI / 2 / N)
   NEXT K
   LET  B(I) = INT(S * SQR(2 / N) + .5)
NEXT I
END SUB

EXTERNAL  SUB DCT4 (A(), N, B())
FOR K = 0 TO N - 1
   LET  S = 0
   FOR I = 0 TO N - 1
      LET  S = S + A(I) * COS((2 * I + 1) * (2 * K + 1) * PI / 4 / N)
      LET  B(K) = S * SQR(2 / N)
   NEXT I
NEXT K
END SUB

EXTERNAL  SUB DCT2D(S(,),N) !'2次元DCT
OPTION BASE 0
DIM X(N), Y(N)
FOR J = 0 TO N - 1
   FOR I = 0 TO N - 1
      LET  X(I) = S(I, J)
   NEXT I
   CALL DCT2 (X, N, Y)
   FOR I = 0 TO N - 1
      LET  S(I, J) = Y(I)
   NEXT I
NEXT J
FOR J = 0 TO N - 1
   FOR I = 0 TO N - 1
      LET  X(I) = S(J, I)
   NEXT I
   CALL DCT2 (X, N, Y)
   FOR I = 0 TO N - 1
      LET  S(J, I) = Y(I)
   NEXT I
NEXT J
END SUB

EXTERNAL  SUB IDCT2D(S(,), N) !'2次元逆DCT
OPTION BASE 0
DIM X(N), Y(N)
FOR J = 0 TO N - 1
   FOR I = 0 TO N - 1
      LET  X(I) = S(I, J)
   NEXT I
   CALL DCT3 (X, N, Y)
   FOR I = 0 TO N - 1
      LET  S(I, J) = Y(I)
   NEXT I
NEXT J
FOR J = 0 TO N - 1
   FOR I = 0 TO N - 1
      LET  X(I) = S(J, I)
   NEXT I
   CALL DCT3 (X, N, Y)
   FOR I = 0 TO N - 1
      LET  S(J, I) = Y(I)
   NEXT I
NEXT J
END SUB

EXTERNAL  FUNCTION CC(X)
IF X=0 THEN LET CC=SQR(2)/2 ELSE LET CC=1
END FUNCTION

EXTERNAL  SUB DCT2D_2(X(,),K) !'2次元DCT
OPTION BASE 0
DIM Y(K-1,K-1)
FOR V=0 TO K-1
   FOR U=0 TO K-1
      LET YY=2/K*C(U)*C(V)
      FOR MM=0 TO K-1
         FOR NN=0 TO K-1
            LET Y(U,V)=Y(U,V)+YY*X(MM,NN)*COS((2*MM+1)*U*PI/2/K)*COS((2*NN+1)*V*PI/2/K)
         NEXT  NN
      NEXT  MM
   NEXT U
NEXT V
MAT X=Y
END SUB

EXTERNAL  SUB IDCT2D_2(X(,),K) !'2次元逆DCT
OPTION BASE 0
DIM Y(K-1,K-1)
FOR MM=0 TO K-1
   FOR NN=0 TO K-1
      LET YY=2/K
      FOR V=0 TO K-1
         FOR U=0 TO K-1
            LET Y(MM,NN)=Y(MM,NN)+YY*C(U)*C(V)*X(U,V)*COS((2*MM+1)*U*PI/2/K)*COS((2*NN+1)*V*PI/2/K)
         NEXT U
      NEXT V
   NEXT  NN
NEXT  MM
MAT X=Y
END SUB
 

戻る