1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| # include<stdio.h> # include<math.h> # include<stdlib.h> # include<time.h> # define N 8 # define PI 3.1415926
int choose = 0; int i,j; double *f = NULL; double *A = NULL; double *AT = NULL;
void dct2(double *,double *); void f_dct2(double *,double *); void printM(double *,int,char *); void mulMatrix(double *P,double *Q,double *resM,int); double *TMatrix(double *, int);
void showMenu(){ printf("\n\n----------请选择----------\n"); printf("\t1. IMgae-a\n"); printf("\t2. IMgae-c\n"); printf("\t3. IMgae-e\n"); printf("\t4. exit\n"); printf("----------endMenu----------\n"); } void executeFn();
int main(){ while(1){ showMenu(); scanf("%d",&choose); if(choose == 4) break; f = (double *)malloc(N*N*sizeof(double)); A = (double *)malloc(N*N*sizeof(double)); AT = (double *)malloc(N*N*sizeof(double)); if(choose == 1){ for(i = 0;i < N;++i) for(j = 0;j < N;++j){ if(j < 4) f[i*N+j] = 0; else f[i*N+j] = 1; } executeFn(); }else if(choose == 2){ for(i = 0;i < N;++i) for(j = 0;j < N;++j){ if(i < 4) f[i*N+j] = 0; else f[i*N+j] = 1; } executeFn(); }else if(choose == 3){ for(i = 0;i < N;++i) for(j = 0;j < N;++j){ if(i%2 == 0){ if(j%2 == 0) f[i*N+j] = 0; else f[i*N+j] = 1; } else{ if(j%2 == 1) f[i*N+j] = 0; else f[i*N+j] = 1; } } executeFn(); } }
return 0; }
void executeFn(){ printM(f,N,"f(原矩阵)=\n"); double x0 = sqrt(1.0/N); double x1 = sqrt(2.0/N); for(i = 0;i < N;++i){ for(j = 0;j < N;++j){ if(i == 0){ A[i*N+j] = x0*cos((2*j+1)*PI*i/(2*N)); }else{ A[i*N+j] = x1*cos((2*j+1)*PI*i/(2*N)); } } } AT = TMatrix(A,N); double *dct = (double *)malloc(N*N*sizeof(double)); dct2(f,dct); printM(dct,N,"dct正变换矩阵=\n"); double *f_dct = (double *)malloc(N*N*sizeof(double)); f_dct2(dct,f_dct); printM(f_dct,N,"dct反变换矩阵=\n"); free(A); free(f); free(dct); free(AT); }
void f_dct2(double *in,double *out){ double *tmp = (double *)malloc(N*N*sizeof(double)); mulMatrix(AT,in,tmp,N); mulMatrix(tmp,A,out,N); }
void dct2(double *in,double *out){ double *tt = (double *)malloc(N*N*sizeof(double)); mulMatrix(A,in,tt,N); mulMatrix(tt,AT,out,N); free(tt); }
void mulMatrix(double *P,double *Q,double *resM,int n){ int t; double res; for(i = 0;i < n;++i){ for(t = 0;t < n;++t){ res = 0; for(j = 0;j < n;++j) res += P[i*N+j]*Q[j*N+t]; resM[i*N+t] = res; } } }
void printM(double *M,int n,char *str){ printf("%s",str);
for(i = 0;i < n;++i){ printf("\t"); for(j = 0;j < n;++j) printf("%.2lf\t",M[i*N+j]); printf("\n"); } }
double *TMatrix(double *M, int n){ double *tmp = (double *)malloc(N*N*sizeof(double)); for(j = 0;j < N;++j) for(i = 0;i < N;++i) tmp[i*N+j] = M[j*N+i]; return tmp; }
|