GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Mar-2004, 14:47
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road

Problem multiplying arrays


Hello,

I have what seems to be a really simple problem, but I've been unable to solve it in two days.

I need to do the following:

Find the autocorrelation of an array, which is part of a two-dimensional array.
The array is of type "double", and is of length 512.

When I print the arrays, SegmentArray[0][256+j] and SegmentArray[0][i+j], I get the correct values.

However, when I print AutocorrArray, which should be nothing but a sum of products as described in the code below, all I get is 0. This is not the correct answer for the array in question, as has been verified by Matlab and Mathcad.

What am I doing wrong?

Thank you,
CPP / C++ / C Code:
for ( i = 0; i < 256; i++ )
{
	Sum = 0.f;
	for ( j = 0; j < 256; j++ )
	{
		Sum += SegmentArray[0][256+j]*SegmentArray[0][i+j];
	}
	AutocorrArray[0][i] = Sum/256;
}
  #2  
Old 29-Mar-2004, 14:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
Hello,

I have what seems to be a really simple problem, but I've been unable to solve it in two days.

I need to do the following:

Find the autocorrelation of an array, which is part of a two-dimensional array.
The array is of type "double", and is of length 512.

When I print the arrays, SegmentArray[0][256+j] and SegmentArray[0][i+j], I get the correct values.

However, when I print AutocorrArray, which should be nothing but a sum of products as described in the code below, all I get is 0. This is not the correct answer for the array in question, as has been verified by Matlab and Mathcad.

What am I doing wrong?

Thank you,
CPP / C++ / C Code:
for ( i = 0; i < 256; i++ )
{
	Sum = 0.f;
	for ( j = 0; j < 256; j++ )
	{
		Sum += SegmentArray[0][256+j]*SegmentArray[0][i+j];
	}
	AutocorrArray[0][i] = Sum/256;
}


What is the type of variable Sum?

Post a program showing declarations.

Dave
  #3  
Old 29-Mar-2004, 15:00
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by davekw7x
What is the type of variable Sum?

Post a program showing declarations.

Dave

CPP / C++ / C Code:
/* Preprocessor Statements */

#include <stdio.h>

FILE *FileDataIn, *FileDataOut, *FileSegment, *FileAutocorr;

double InputArray[1280];
//This program examines the case for one segment.  In the final program there will be 7 segments and will need double SegmentArray[7][512]
double SegmentArray[512];
// This will need to be changed to double AutocorrArray[7][256]
double AutocorrArray[256];

int main()
{
	int i = 0, j = 0;
	double Sum = 0;
		
	// Step 0: Read the .wav data values from a text file
	
	if ( ( FileDataIn = fopen("WaveData.txt","r") ) == NULL )
	{
		printf("File not found \n");
		getchar();
		exit(1);
	}
	else
	{
	  i = 0;
	  while(!feof(FileDataIn))
	  { 
      // loop through and store the numbers into the array
      fscanf(FileDataIn, "%d", &InputArray[i]);
      i++;
      }	  
	  printf("File successfully read \n");
    }
    fclose(FileDataIn);
    
        
    // Step 1: Segmentation
    
    for ( i = 0; i < 512; i++ )
    {
    	SegmentArray[i] = InputArray[i];
    }
    
    printf("Segmentation Complete\n");
   
    // Write Segment Data
 
    if ( ( FileSegment = fopen("Segment.txt", "w") ) == NULL ) 
    {
     	printf("Error: can't create file.\n");
     	exit(0);
    }
    else
    {
    	for ( i = 0; i < 512; i ++ )
    	{
    		fprintf(FileSegment, "%d\n", SegmentArray[i]);
    	}
    	printf("Segment successfully written\n");      	  
    }
    fclose(FileSegment);    
  
	

	printf("\nPrinting SegmentArray[i]\n");
	for (i = 0; i < 10; i++)
	{
		printf("%d\n", SegmentArray[256+i]);
	}
	
	printf("\nPrinting SegmentArray[i+j]\n");
	for (j = 0; j < 10; j++)
	{
		printf("%d\n", SegmentArray[j]);
	}
	
	// AUTOCORRELATION

	for ( i = 0; i < 256; i++ )
	{
		Sum = 0;
		for ( j = 0; j < 256; j++ )
		{
			Sum += SegmentArray[256+j]*SegmentArray[i+j];
		}
		AutocorrArray[i] = Sum/256;
	}
		
	if ( ( FileAutocorr = fopen("Autocorr.txt", "w") ) == NULL ) 
    {
     	printf("Error: can't create file.\n");
     	exit(0);
    }
    else
    {
    	for ( i = 0; i < 256; i ++ )
    	{
    		fprintf(FileAutocorr, "%d\n", AutocorrArray[i]);
    	}
    	printf("Autocorr successfully written\n");      	  
    }
    fclose(FileAutocorr);

	printf("\n End of Routines \n"); 
} 

My problem is that the contents of AutocorrArray are ALL 0.
Last edited by dsmith : 29-Mar-2004 at 15:08. Reason: Fixed C syntax tag
  #4  
Old 29-Mar-2004, 15:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Could you supply (attach) the WaveData.txt file that you are using?


Dave
  #5  
Old 29-Mar-2004, 15:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by davekw7x
Could you supply (attach) the WaveData.txt file that you are using?


Dave


You have the following:
CPP / C++ / C Code:
      fscanf(FileDataIn, "%d", &InputArray[i]);

but InputArray is declared
CPP / C++ / C Code:
double InputArray[1280];

So fscanf() should use "%lf", I think.


Are you sure you have read the input correctly? Hint: try printing out the first 10 or so (no need to print the entire array).

Dave
  #6  
Old 29-Mar-2004, 15:14
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by davekw7x
Could you supply (attach) the WaveData.txt file that you are using?


Dave

Here it is!

Thanks, Dave.
Attached Files
File Type: txt Wavedata.txt (8.3 KB, 13 views)
  #7  
Old 29-Mar-2004, 15:17
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by davekw7x
You have the following:
CPP / C++ / C Code:
      fscanf(FileDataIn, "%d", &InputArray[i]);

but InputArray is delcared
CPP / C++ / C Code:
double InputArray[1280];

Are you sure you have read the input correctly? Hint: try printing out the first 10 or so (no need to print the entire array).

Dave

The input's been read correctly. I used both %f and %d, and verified (both by printing to the screen and to an output file) that the contents of InputArray are the same as "WaveData.txt".
  #8  
Old 29-Mar-2004, 15:24
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by davekw7x
You have the following:
CPP / C++ / C Code:
      fscanf(FileDataIn, "%d", &InputArray[i]);

but InputArray is declared
CPP / C++ / C Code:
double InputArray[1280];

So fscanf() should use "%lf", I think.


Are you sure you have read the input correctly? Hint: try printing out the first 10 or so (no need to print the entire array).

Dave

DAVE, YOU ROCK!

I changed all the %ds to %lf, and I got it! What exactly is %lf anyway? Sorry, I'm a newbie!

Thanks a lot!
  #9  
Old 29-Mar-2004, 15:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
The input's been read correctly. I used both %f and %d, and verified (both by printing to the screen and to an output file) that the contents of InputArray are the same as "WaveData.txt".


You declare InputArray as double so you must (must) use %lf in scanf(), not %d, not %f.

Also in your printf() statements for SegmentArray and AutocorrArray, you must not use %d (OK to use %f or %lf in printf(), since the argument will be promoted to double, anyhow. For scanf(), must use %lf.)

Please run again, and put the following after you close WaveData.txt:

CPP / C++ / C Code:
  for (i = 0; i < 10; i++) {
    printf("<%f> ", InputArray[i]);
  }
  printf("\n");

Dave
  #10  
Old 29-Mar-2004, 15:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
I changed all the %ds to %lf, and I got it! What exactly is %lf anyway? Sorry, I'm a newbie!

Thanks a lot!

You are giving scanf() the address of a variable. Scanf() will convert the appropriate number of input bytes and store in the place you tell it.

%f indicates to scanf that this is a float type variable.
%lf indicates to scanf() that this is a double type variable (lf is mnemonic for "long float", I guess).

By the way, C does all internal float math operations as double, so there's no advantage to float (as opposed to double), unless there are large arrays and storage becomes an issue.

Regards,

Dave
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 09:11
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 10:44
Geforce FX 5600 Ultra problem jenovasbirth Computer Hardware Forum 1 04-Oct-2003 17:00
folder problem in trees zuzupus MySQL / PHP Forum 23 26-Sep-2003 07:32
link problem [1][2]----- zuzupus MySQL / PHP Forum 0 16-Sep-2003 05:16

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

All times are GMT -6. The time now is 17:56.


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