GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Apr-2004, 05:02
oshiotse oshiotse is offline
New Member
 
Join Date: Apr 2004
Posts: 14
oshiotse is on a distinguished road

using vector or array


I have a file of about 446604bytes. I want to know the frequency distribution of the file using a vector or an array. i have already tried using array but i don't think it is write. here is past of the code
CPP / C++ / C Code:
void Wavelet ::freqdist4()
{
	int count, found; 
	
	double myarray[2189]; // 2189 is theTotal number of 
							// specific number after counting

	int freq[2189] ;	//


	count = 1;
	int k = 0;
	myarray[0] = datatrans4[0];
	for(int di = (datasize/16) + 1; di< (datasize/8); di++)
	{
		while ((k < di) && (datatrans4[di] != datatrans4[k-1]))
		{
			if (datatrans4[di] == datatrans4[k]) found = 0;
			else found = 1;
			k= k+1;
		}
		k= 0;
		if (found == 1 )
		{
			count = count + 1;
			myarray[count - 1] = datatrans4[di];
		}

	}

	for (int dc = 0; dc < count; dc++ )
	{
		freq[dc]= 0;
	}

	for (int dd =0; dd< count ; dd++ )
	{
		for (int de = (datasize/16) + 1; de< (datasize/8); de++ )
		{
			if (myarray[dd] == datatrans4[de])
				freq[dd]++;
		}
		cout<<myarray[dd] <<"            " <<freq[dd]<<"   ";
		cout<<""<<endl;
	}
	cout<<endl;
	
}

can someone pls help me . thanks
  #2  
Old 15-Apr-2004, 10:18
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi oshiotse. Welcome to GIDForums. I don't see why the array wouldn't work just as good as a vector. However, can you let us know why or what doesn't work with the code? Without having the complete program and a file it is kind of hard to divine what is "wrong" with the code.

Cheers,
d
  #3  
Old 15-Apr-2004, 22:23
vadharah vadharah is offline
Junior Member
 
Join Date: Apr 2004
Posts: 45
vadharah is on a distinguished road
Quote:
Originally Posted by oshiotse
I have a file of about 446604bytes. I want to know the frequency distribution of the file using a vector or an array. i have already tried using array but i don't think it is write. here is past of the code
CPP / C++ / C Code:
void Wavelet ::freqdist4()
{
	int count, found; 
	
	double myarray[2189]; // 2189 is theTotal number of 
							// specific number after counting

	int freq[2189] ;	//


	count = 1;
	int k = 0;
	myarray[0] = datatrans4[0];
	for(int di = (datasize/16) + 1; di< (datasize/8); di++)
	{
		while ((k < di) && (datatrans4[di] != datatrans4[k-1]))
		{
			if (datatrans4[di] == datatrans4[k]) found = 0;
			else found = 1;
			k= k+1;
		}
		k= 0;
		if (found == 1 )
		{
			count = count + 1;
			myarray[count - 1] = datatrans4[di];
		}

	}

	for (int dc = 0; dc < count; dc++ )
	{
		freq[dc]= 0;
	}

	for (int dd =0; dd< count ; dd++ )
	{
		for (int de = (datasize/16) + 1; de< (datasize/8); de++ )
		{
			if (myarray[dd] == datatrans4[de])
				freq[dd]++;
		}
		cout<<myarray[dd] <<"            " <<freq[dd]<<"   ";
		cout<<""<<endl;
	}
	cout<<endl;
	
}

can someone pls help me . thanks

we have all seen ur problem but if u could be more specific tell us whats going wrong..u jus never know!
  #4  
Old 16-Apr-2004, 03:35
oshiotse oshiotse is offline
New Member
 
Join Date: Apr 2004
Posts: 14
oshiotse is on a distinguished road

using vectors or array


Quote:
Originally Posted by dsmith
Hi oshiotse. Welcome to GIDForums. I don't see why the array wouldn't work just as good as a vector. However, can you let us know why or what doesn't work with the code? Without having the complete program and a file it is kind of hard to divine what is "wrong" with the code.

Cheers,
d

This is a copy of the entire program
[c]

#include "stdafx.h"
#include "Wavelet.h"
#include <iostream>
#include <stdio.h>
using namespace std;
#include <string>
#include <fstream>


FILE *input, *output;

Wavelet ::wavelet()
{

}

void Wavelet::ReadFromFile()
{
if ( (input = fopen( "vic2.wav", "rb+")) != NULL)
cout<<"In File opened successfully\n";

// r e a d i n g the word "RIFF"
fread (csid, 4, 1, input);
for (int i=0; i<4; i++)
cout<< csid[i];
cout << endl;



//R e a d i n g f i l e s i z e
fread((char*)&fsize,4,1,input);
cout<<"File size is: "<<fsize<<"\n";


//form typeID

// r e a d i n g the word "WAVE"
fread (csid1, 4, 1, input);
for (int j=0; j<4; j++)
cout<< csid1[j];
cout <<"\n";



//Second"Chunk"ID
// r e a d i n g the word "fmt "
fread (csid2, 4, 1, input);
for (int a=0; a<4; a++)
cout<< csid2[a];
cout<<"\n";


//cout<<(char*)&wfxsize<<"byte"<<"\n";

//Wave Format Size
fread((char*)&wfxsize,4,1,input);
cout<<"Check it out: "<<wfxsize<<" byte"<<"\n";



//Wave Format Info
fread(pwfx,18,1,input);
cout<<"Wave Formt Info: "<<pwfx<<" byte"<<"\n";

//Wave Format Info:fact
fread (csid3, 4, 1, input);
for (int h=0; h<4; h++)
cout<< csid3[h];
cout <<"\n";

//Wave Format Info
fread((char*)&datasiz,4,1,input);
cout<<datasiz<<"byte"<<"\n";

//Wave Format Info
databuf = new char [datasiz];
fread (databuf,datasiz, 1, input);
cout<<databuf<<"\n";

//Reading the word "data"
fread (csid4, 4, 1, input);
for (int p=0; p<4; p++)
cout<< csid4[p];
cout <<"\n";

//Datasize of the PCM data
fread((char*)&datasize,4,1,input);
cout<<"The data sample size is:"<<datasize<<" bytes"<<"\n";

datap = new unsigned char[datasize];
fread(datap, datasize, 1, input);

min= datap[datasize];
cout<<min<<endl;

datatrans = new double[datasize];

int Low,High;
Low=0; High=datasize/2;

//datatrans[Low] = (datap[0] + datap[1])*0.7071;
//min1 = datatrans[Low];
//max1 = datatrans[Low];
//Low =1;



// The first part of the wavelet, implemented on datap which contains
// the original data

for(int y = 0; y < datasize-1 ; y+=2)
{
datatrans[Low++]=(datap[y] + datap[y+1])*0.7071;
datatrans[High++] =(datap[y]-datap[y+1])*0.7071;

}


// The The second part of the wavelet, implemented on datatrans
datatrans2 = new double[datasize];
Low=0; High=datasize/4;

for(int yy = 0; yy < (datasize/2)-1 ; yy+=2)
{
datatrans2[Low++]=(datatrans[yy] + datatrans[yy+1])*0.7071;
datatrans2[High++] =(datatrans[yy]-datatrans[yy+1])*0.7071;
}

for(int yyy = (datasize/2)+1; yyy < datasize ; yyy++)
{
datatrans2[yyy]=datatrans[yyy];
}


// The third part of the wavelet, implemented on datatrans2.


datatrans3 = new double[datasize];
Low=0; High=datasize/8;


for(int q = 0; q < (datasize/4)-1 ; q+=2)
{
datatrans3[Low++]=(datatrans2[q] + datatrans2[q+1])*0.7071;
datatrans3[High++] =(datatrans2[q]-datatrans2[q+1])*0.7071;
}

for(int s = (datasize/4)+1; s < datasize ; s++)
{
datatrans3[s]=datatrans2[s];
}



// The fouth part of the wavelet, implemented on datatrans3.

datatrans4 = new double[datasize];
Low=0; High=datasize/16;


for(int t = 0; t < (datasize/-1 ; t+=2)
{
datatrans4[Low++]=(datatrans3[t] + datatrans3[t+1])*0.7071;
datatrans4[High++] =(datatrans3[t]-datatrans3[t+1])*0.7071;
}

for(int f = (datasize/+1; f < datasize ; f++)
{
datatrans4[f]=datatrans3[f];
}




// The first part of the coversion.This convert from datatrans4 to datatran3

datat = new double[datasize];

Low=0; High=datasize/16;
for(int z = 0; z < (datasize/-1 ; z+=2)
{
datat[z] = (datatrans4[Low++] + datatrans4[High++])/(2*0.7071);
Low = Low - 1; High= High - 1;
datat[z+1] = (datatrans4[Low++] - datatrans4[High++])/(2*0.7071);
}

for(int zz = (datasize/+1; zz < datasize ; zz++)
{
datat[zz]=datatrans4[zz];
}

// The second part of the coversion.This convert from datatrans3 to datatran2

datat2 = new double[datasize];

Low=0; High=datasize/8;
for(int zzz = 0; zzz < (datasize/4)-1 ; zzz+=2)
{
datat2[zzz] = (datat[Low++] + datat[High++])/(2*0.7071);
Low = Low - 1; High= High - 1;
datat2[zzz+1] = (datat[Low++] - datat[High++])/(2*0.7071);
}

for(int w = (datasize/4)+1; w < datasize ; w++)
{
datat2[w]=datat[w];
}

// The third part of the coversion.This convert from datatrans2 to datatran

datat3 = new double[datasize];

Low=0; High=datasize/4;
for(int ww = 0; ww < (datasize/2)-1 ; ww+=2)
{
datat3[ww] = (datat2[Low++] + datat2[High++])/(2*0.7071);
Low = Low - 1; High= High - 1;
datat3[ww+1] = (datat2[Low++] - datat2[High++])/(2*0.7071);
}

for(int tt = (datasize/2)+1; tt < datasize ; tt++)
{
datat3[tt]=datat2[tt];
}



// The third part of the coversion.This convert from datatrans to datap
// Remember that datap contains the original data

datat4 = new unsigned char[datasize];

Low=0; High=datasize/2;
for(int bb = 0; bb < (datasize)-1 ; bb+=2)
{
datat4[bb] = (datat3[Low++] + datat3[High++])/(2*0.7071);
Low = Low - 1; High= High - 1;
datat4[bb+1] = (datat3[Low++] - datat3[High++])/(2*0.7071);
}


// The two parts below are done to enhanced the listening
// it should be removed when doing the other transformation
/*
//(1) Find the min and max in Low band
min1=max1=datatrans[0];
for (int mm=1;mm<datasize/2;mm++)
{
if (min1>datatrans[mm]) min1 = datatrans[mm];
if (max1<datatrans[mm]) max1 = datatrans[mm];
}
cout<<min1<<" min for low "<<endl;
cout<<max1<<" max for low "<<endl;

//Scale the values to be in [0-255]
double m=255/(double)(max1-min1);
for (int s=0;s<datasize/2;s++){
datatrans[s]=m*(datatrans[s]-min1);
}


//(2) Find the min and max in High band
min1=max1=datatrans[(datasize/2)+1];
for (int hh=(datasize/2)+1;hh<datasize;hh++)
{
if (min1>datatrans[hh]) min1 = datatrans[hh];
if (max1<datatrans[hh]) max1 = datatrans[hh];
}
cout<<min1<<" min for High "<<endl;
cout<<max1<<" max for High "<<endl;

//Scale the values to be in [0-255]
m=255/(double)(max1-min1);
for (int ss=(datasize/2)+1;ss<datasize;ss++){
datatrans[ss]=m*(datatrans[ss]-min1);
}


*/
}



void Wavelet ::Write2file()
{

if( (output = fopen ( "vicout1.wav", "wb+")) != NULL)
cout<<"\n\nOut File opened successfully\n";
//fwrite (header, 55200,1,output);
fwrite (csid, 4, 1, output);
for (int i=0; i<4; i++)
cout<< csid[i];
cout <<endl;

fwrite((char*)&fsize,4,1,output);
cout<<fsize<<"file size"<<"\n";

fwrite(csid1, 4, 1, output);
for (int j=0; j<4; j++)
cout<< csid1[j];
cout <<"\n";

fwrite (csid2, 4, 1, output);
for (int a=0; a<4; a++)
cout<< csid2[a];
cout <<"\n";

fwrite((char*)&wfxsize,4,1,output);
cout<<wfxsize<<"byte"<<"\n";

fwrite((char*)&pwfx,wfxsize,1,output);
cout<<pwfx<<"byte"<<"\n";

fwrite (csid3, 4, 1, output);
for (int b=0; b<4; b++)
cout<< csid3[b];
cout <<"\n";

fwrite((char*)&datasiz,4,1,output);
cout<<datasiz<<"byte"<<"\n";

//databuf = new char [datasiz];
fwrite (databuf,datasiz, 1, output);
cout<<databuf<<"\n";

fwrite (csid4, 4, 1, output);
for (int c=0; c<4; c++)
cout<< csid4[c];
cout <<"\n";

fwrite((char*)&datasize,4,1,output);
cout<<datasize<<"byte"<<"\n";

// convert the data from double to bytes
for(int f=0;f<datasize;f++)
datap[f]=(unsigned char)datatrans[f];

fwrite (datap, datasize,1,output);
fclose (output);

}

void Wavelet::Write2filestg2()
{

if( (output = fopen ( "vicout2.wav", "wb+")) != NULL)
cout<<"\n\nOut File opened successfully\n";
//fwrite (header, 55200,1,output);

fwrite (csid, 4, 1, output);
for (int i=0; i<4; i++)
cout<< csid[i];
cout <<endl;

fwrite((char*)&fsize,4,1,output);
cout<<fsize<<"file size"<<"\n";

fwrite(csid1, 4, 1, output);
for (int j=0; j<4; j++)
cout<< csid1[j];
cout <<"\n";

fwrite (csid2, 4, 1, output);
for (int a=0; a<4; a++)
cout<< csid2[a];
cout <<"\n";

fwrite((char*)&wfxsize,4,1,output);
cout<<wfxsize<<"byte"<<"\n";

fwrite((char*)&pwfx,18,1,output);
cout<<pwfx<<"byte"<<"\n";

fwrite (csid3, 4, 1, output);
for (int b=0; b<4; b++)
cout<< csid3[b];
cout <<"\n";

fwrite((char*)&datasiz,4,1,output);
cout<<datasiz<<"byte"<<"\n";

//databuf = new char [datasiz];
fwrite (databuf,datasiz, 1, output);
cout<<databuf<<"\n";

fwrite (csid4, 4, 1, output);
for (int c=0; c<4; c++)
cout<< csid4[c];
cout <<"\n";

fwrite((char*)&datasize,4,1,output);
cout<<datasize<<"byte"<<"\n";

// convert the data from double to bytes
for(int f=0;f<datasize;f++)
datap[f]=(unsigned char)datatrans2[f];

fwrite (datap, datasize,1,output);
fclose (output);

}


void Wavelet::Write2filestg3()
{

if( (output = fopen ( "vicout3.wav", "wb+")) != NULL)
cout<<"\n\nOut File opened successfully\n";
//fwrite (header, 55200,1,output);
fwrite (csid, 4, 1, output);
for (int i=0; i<4; i++)
cout<< csid[i];
cout <<endl;

fwrite((char*)&fsize,4,1,output);
cout<<fsize<<"file size"<<"\n";

fwrite(csid1, 4, 1, output);
for (int j=0; j<4; j++)
cout<< csid1[j];
cout <<"\n";

fwrite (csid2, 4, 1, output);
for (int a=0; a<4; a++)
cout<< csid2[a];
cout <<"\n";

fwrite((char*)&wfxsize,4,1,output);
cout<<wfxsize<<"byte"<<"\n";

fwrite((char*)&pwfx,18,1,output);
cout<<pwfx<<"byte"<<"\n";

fwrite (csid3, 4, 1, output);
for (int b=0; b<4; b++)
cout<< csid3[b];
cout <<"\n";

fwrite((char*)&datasiz,4,1,output);
cout<<datasiz<<"byte"<<"\n";

//databuf = new char [datasiz];
fwrite (databuf,datasiz, 1, output);
cout<<databuf<<"\n";

fwrite (csid4, 4, 1, output);
for (int c=0; c<4; c++)
cout<< csid4[c];
cout <<"\n";

fwrite((char*)&datasize,4,1,output);
cout<<datasize<<"byte"<<"\n";

// convert the data from double to bytes
for(int f=0;f<datasize;f++)
datap[f]=(unsigned char)datatrans3[f];

fwrite (datap, datasize,1,output);
fclose (output);

}

void Wavelet ::Write2filestg4()
{

if( (output = fopen ( "vicout4.wav", "wb+")) != NULL)
cout<<"\n\nOut File opened successfully\n";
//fwrite (header, 55200,1,output);
fwrite (csid, 4, 1, output);
for (int i=0; i<4; i++)
cout<< csid[i];
cout <<endl;

fwrite((char*)&fsize,4,1,output);
cout<<fsize<<"file size"<<"\n";

fwrite(csid1, 4, 1, output);
for (int j=0; j<4; j++)
cout<< csid1[j];
cout <<"\n";

fwrite (csid2, 4, 1, output);
for (int a=0; a<4; a++)
cout<< csid2[a];
cout <<"\n";

fwrite((char*)&wfxsize,4,1,output);
cout<<wfxsize<<"byte"<<"\n";

fwrite((char*)&pwfx,18,1,output);
cout<<pwfx<<"byte"<<"\n";

fwrite (csid3, 4, 1, output);
for (int b=0; b<4; b++)
cout<< csid3[b];
cout <<"\n";

fwrite((char*)&datasiz,4,1,output);
cout<<datasiz<<"byte"<<"\n";

//databuf = new char [datasiz];
fwrite (databuf,datasiz, 1, output);
cout<<databuf<<"\n";

fwrite (csid4, 4, 1, output);
for (int c=0; c<4; c++)
cout<< csid4[c];
cout <<"\n";

fwrite((char*)&datasize,4,1,output);
cout<<datasize<<"byte"<<"\n";

// convert the data from double to bytes
for(int f=0;f<datasize;f++)
datap[f]=(unsigned char)datatrans4[f];

fwrite (datap, datasize,1,output);
fclose (output);

}
void Wavelet::Write2fileconv()
{

if( (output = fopen ( "vicoutconv.wav", "wb+")) != NULL)
cout<<"\n\nOut File opened successfully\n";
//fwrite (header, 55200,1,output);
fwrite (csid, 4, 1, output);
for (int i=0; i<4; i++)
cout<< csid[i];
cout <<endl;

fwrite((char*)&fsize,4,1,output);
cout<<fsize<<"file size"<<"\n";

fwrite(csid1, 4, 1, output);
for (int j=0; j<4; j++)
cout<< csid1[j];
cout <<"\n";

fwrite (csid2, 4, 1, output);
for (int a=0; a<4; a++)
cout<< csid2[a];
cout <<"\n";

fwrite((char*)&wfxsize,4,1,output);
cout<<wfxsize<<"byte"<<"\n";

fwrite((char*)&pwfx,18,1,output);
cout<<pwfx<<"byte"<<"\n";

fwrite (csid3, 4, 1, output);
for (int b=0; b<4; b++)
cout<< csid3[b];
cout <<"\n";

fwrite((char*)&datasiz,4,1,output);
cout<<datasiz<<"byte"<<"\n";

//databuf = new char [datasiz];
fwrite (databuf,datasiz, 1, output);
cout<<databuf<<"\n";

fwrite (csid4, 4, 1, output);
for (int c=0; c<4; c++)
cout<< csid4
CPP / C++ / C Code:
;
	cout <<"\n";
	
	fwrite((char*)&datasize,4,1,output);
	cout<<datasize<<"byte"<<"\n";

	// convert the data from double to bytes
	for(int f=0;f<datasize;f++)
		datap[f]=(unsigned char)datat4[f];

	fwrite (datap, datasize,1,output);
	fclose (output);

	cout<<endl;
	
}

void Wavelet::compare()
{
	for (int se =0; se < datasize; se++)
	{
		if (datap[se] != datat4[se])
		cout<<datap<<"  "<<datat4;
	}
	cout<<"The data before and after convertion are thesame\n"<<endl;
	
}



void Wavelet ::shifstg4()
{
	int bol = (datasize/16)+1;
	min = datatrans4[bol];
	for (bol=(datasize/16) + 2; bol< (datasize/8); bol++)
	{
		if (min > datatrans4[bol]) min = datatrans4[bol];
	}
	cout<<"MIN at At 4 = "<<min<<endl;


	int l = (datasize/16) + 1;
	for(l=(datasize/16) + 1; l< (datasize/8); l++)
	{
		if (datatrans4[l]<0) datatrans4[l] = datatrans4[l] - min;
	}

	int bo = (datasize/16)+1;
	min1 = datatrans4[bo];
	for (bo=(datasize/16) + 2; bo< (datasize/8); bo++)
	{
		if (min1 > datatrans4[bo]) min1 = datatrans4[bo];
	}
	cout<<"New min at stage 4 = "<<min1<<endl;

}

void Wavelet ::freqdist4()
{
	int count, found; 
	
	double myarray[2189]; // 2189 is theTotal number of 
							// specific number after counting

	double freq[2189] ;	//


	count = 1;
	int k = 0;
	myarray[0] = datatrans4[0];
	for(int di = (datasize/16) + 1; di< (datasize/8); di++)
	{
		while ((k < di) && (datatrans4[di] != datatrans4[k-1]))
		{
			if (datatrans4[di] == datatrans4[k]) found = 0;
			else found = 1;
			k= k+1;
		}
		k= 0;
		if (found == 1 )
		{
			count = count + 1;
			myarray[count - 1] = datatrans4[di];
		}

	}

	for (int dc = 0; dc < count; dc++ )
	{
		freq[dc]= 0;
	}

	for (int dd =0; dd< count ; dd++ )
	{
		for (int de = (datasize/16) + 1; de< (datasize/8); de++ )
		{
			if (myarray[dd] == datatrans4[de])
				freq[dd]++;
		}
		cout<<myarray[dd] <<"            " <<freq[dd]<<"   ";
		cout<<""<<endl;
	}
	cout<<endl;
	
}


Pls Remember that i want to use vector to get the frequency distribution. i have already used an array but i don't think it working properly.

Thanks
  #5  
Old 16-Apr-2004, 10:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
dsmith wrote: Hi oshiotse. Welcome to GIDForums. I don't see why the array wouldn't work just as good as a vector. However, can you let us know why or what doesn't work with the code? Without having the complete program and a file it is kind of hard to divine what is "wrong" with the code.

vadharah wrote: we have all seen ur problem but if u could be more specific tell us whats going wrong..u jus never know!

In response you posted almost 700 lines of code with no comment, no explanation. We aren't likely to try to understand a program this large looking for an unknown problem in an unknown section of code.

Be Specific! What is wrong? Where is the problem? What should it be doing instead?
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:05.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.