#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 3.14159265
int **transpose(int **x,int m,int n);
int **matrix_mul(int **m1,int **m2,int r1,int c1,int r2,int c2 );
int **dc4(int **x,int nrows,int ncolumns);
main()
{
printf("hi");
int nrows=80000,ncolumns=1,i,j,**x,**dc4_x,**ix,M=nrows,N=ncolumns;
//memory allocation for x
x = malloc(nrows * sizeof(int *));
if(x == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < nrows; i++)
{
x[i] = malloc(ncolumns * sizeof(int));
if(x[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
printf("hi");
//define some x(will take audio input later)
for(i=0;i<80000;i++)
for(j=0;j<1;j++)
x[i][j]=rand()%3;
for(i=0;i<8;i++)
printf("%d \t",x[i][j]);
//find dc4
dc4_x=dc4(x,80000,1);
//display dc4
for(i=0;i<M;i++)
for(j=0;j<1;j++)
printf("%d",dc4_x[i][j]);
}
//FUNCTION_dc4
int **dc4(int **x,int nrows,int ncolumns)
{
int M,N,i,j,**y,**T,**ix,**cn;
M=nrows;
N=ncolumns; //directly giving the size of x (M,N). will include program to find the size of matrix later.
//memory allocation for y,store dc4
y = malloc(nrows * sizeof(int *));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < nrows; i++)
{
y[i] = malloc(ncolumns * sizeof(int));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
//memory allocation for cn
cn = malloc(nrows * sizeof(int *));
if(cn == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < nrows; i++)
{
cn[i] = malloc(ncolumns * sizeof(int));
if(cn[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
//define cn
cn[0][1]=1;
for(i=0;i<N;i++)
for(j=2;j<M;j++)
{
cn[i][j]=cn[i][j-1]+2;
}
//find cn'(i.e. cn_transpose)
int **cn_transpose;
cn_transpose=transpose(cn,M,N);
//find ix=cn'*cn
ix=matrix_mul(cn_transpose,cn,M,N,N,M);
//memory allocation for T
T = malloc(M * sizeof(int *));
if(T == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
T[i] = malloc(M * sizeof(int));
if(T[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
//define T
for(i=0;i<M;i++)
for(j=0;j<M;j++)
T[i][j]=(1/sqrt(M/2))*cos((pi*ix[i][j])/(4*M));
//find dc4
y=matrix_mul(T,x,M,M,M,1);
//return dc4
return y;
}
//FUNCTION_transpose
int **transpose(int **x,int m,int n)
{
int nrows=n,ncolumns=m,i,j;
//memory allocation for y,to store transpose
int **y;
y = malloc(nrows * sizeof(int *));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < nrows; i++)
{
y[i] = malloc(ncolumns * sizeof(int));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
y[i][j]=x[j][i];
}
return y;
}
//FUNCTION_matrix_mul
int **matrix_mul(int **m1,int **m2,int r1,int c1,int r2,int c2 )
{
int i,j,k;
//memory allocation to store product
int **p;
p = malloc(r1 * sizeof(int *));
if(p == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < r1; i++)
{
p[i] = malloc(c2 * sizeof(int));
if(p[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
//product
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
p[i][j]=0;
for(k=0;k<r2;k++)
{
p[i][j]=p[i][j]+(m1[i][k]*m2[k][j]);
}
}
}
return p;
}
segmentation fault comes. cant detect any errors . help please