473,796 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to improve my function?

Hi,all.

I produce a function to analysis the test data,which is wave signal, and stored as array a[i].
I want to figure out how many times it upcrosses zero,which means that when a[i]<0,and a[i+1]>0,
it upcrosses zero one time.I need store the numbers of upcross zero, and the index where it upcrosses
zero, for example, index i(a[i}<0&&a[i+1]>0) and index j(a[j]<0&&a[j+1]>0).Between a[i] and a[j],
there is no upcross zero signal. The maximum and minimum value between a[i+1] and a[j] need to be found,
and a new array is defined as h[n], which is used to store MAX-MIN between a[i+1] and a[j].So the size of
array h should be NUM-1, where NUM is the numbers of upcross zero times.

But now I can not define the array h with the size NUM-1.

The following is my function, please note the pointer indexH in function upzeroH, and the array indexH in
the main function, where I define it with size large enough as row/2.I want it's size just is wavenums.

Would you please give me a hand? Any suggestion will be helpful.

Thank you for your attention.

Bowlderster.


#include<stdio. h>
#include<stdlib .h>
#include<math.h >
#include<gsl/gsl_sort_double .h>
#include<gsl/gsl_vector.h>

/*Function to figure out upcross zero times
*wavenums----upcross zero times
*indexH---record where upcross zero
*/
int upzeroH(int *indexH,double *inputH,int ninputH)
{
int i,j,nIndex=0;
double *p1,*p2;
double epsilon=0.00000 01;
int nUpzero=0;
for(i=0;i<ninpu tH-1;++i)
{
// printf("i=%i\n" ,i);
p1=&inputH[i];
p2=&inputH[i+1];
while(*p1<-(epsilon)&&*p2> =0)
{
*(indexH+nUpzer o)=i;
//printf("Here upzero:%i\n",i) ;
++nUpzero;
*p1=0;
*p2=0;
}
}

int wavenums=nUpzer o-1;
return wavenums;
}

//define a strcut to record the H and T
struct waveHT
{
//H_max
double waveheight_max;
//H_1/10
double waveheight_1_10 ;
//H_1/3
double waveheight_1_3;
//H_average
double waveheight_aver age;
//T_max
double waveperiod_max;
//T_1/10
double waveperiod_1_10 ;
//T_1/3
double waveperiod_1_3;
//T_average
double waveperiod_aver age;
};
//Function to calculate wave height and period
struct waveHT cal_h_t(double *h,size_t n)
{
struct waveHT wave_h_t;
//H_max
double *tmp_hmax;
tmp_hmax=(doubl e*)malloc(sizeo f(double));
wave_h_t.wavehe ight_max=gsl_so rt_largest(tmp_ hmax,1,h,1,n);
wave_h_t.wavehe ight_max=*tmp_h max;
free(tmp_hmax);

//H_1/10 or H_s
double *tmp_hs;
tmp_hs=(double* )malloc(n/3*sizeof(double ));

free(tmp_hs);
return wave_h_t;
}
int main(void)
{
int i,j,row=10240,c olumn=3;
int numWaves;
int indexH[row/2];
double a[row];
double dt=0.02;

struct waveHT htest;

for(i=0;i<row;+ +i)
{
a[i]=sin(-0.2+i*dt);
}

// numWaves=nUpzer o(a,row);
numWaves=upzero H(indexH,a,row) ;
//printf("There are %i waves \n",numWaves) ;

// for(i=0;i<numWa ves+1;++i)
// printf("%i and Here upcross zero: %i\n",i,*(index H+i));

// printf("size of indexH %i\n",sizeof(in dexH));
double ht[numWaves];

for(i=0;i<numWa ves;++i)
{
// printf("space %i\n",indexH[i+1]-indexH[i]);
gsl_vector *v=gsl_vector_a lloc(indexH[i+1]-indexH[i]);
for(j=0;j<index H[i+1]-indexH[i];++j)
gsl_vector_set( v,j,a[indexH[i]+j]);
ht[i]=gsl_vector_max (v)-gsl_vector_min( v);
// printf("wave height is %g\n",ht[i]);
gsl_vector_free (v);
}

htest=cal_h_t(h t,numWaves);

printf("The largest wave height is %g\n",htest.wav eheight_max);

return 0;
}
Oct 30 '07 #1
2 1385
Bowlderster <bo*********@gm ail.comwrites:
Hi,all.

I produce a function to analysis the test data,which is wave signal,
and stored as array a[i]. I want to figure out how many times it
upcrosses zero,which means that when a[i]<0,and a[i+1]>0, it
upcrosses zero one time.I need store the numbers of upcross zero,
and the index where it upcrosses zero, for example, index
i(a[i}<0&&a[i+1]>0) and index j(a[j]<0&&a[j+1]>0).Between a[i] and
a[j], there is no upcross zero signal. The maximum and minimum value
between a[i+1] and a[j] need to be found, and a new array is defined
as h[n], which is used to store MAX-MIN between a[i+1] and a[j].So
the size of array h should be NUM-1, where NUM is the numbers of
upcross zero times.

But now I can not define the array h with the size NUM-1.
You will have to make two passes though the data or make a guess and
then enlarge the array if you guess too small. You can re-size a
dynamic array using realloc.
int upzeroH(int *indexH,double *inputH,int ninputH)
{
int i,j,nIndex=0;
double *p1,*p2;
double epsilon=0.00000 01;
int nUpzero=0;
for(i=0;i<ninpu tH-1;++i)
{
// printf("i=%i\n" ,i);
p1=&inputH[i];
p2=&inputH[i+1];
while(*p1<-(epsilon)&&*p2> =0)
{
*(indexH+nUpzer o)=i;
Writing 'indexH[nUpzero] = i;' is more idiomatic in C.
//printf("Here upzero:%i\n",i) ;
++nUpzero;
*p1=0;
*p2=0;
}
The while is just an 'if' and the pointers are not required:

if (inputH[i] < -epsilon && inputH[i + 1] >= 0)
indexH[nUpzero++] = i;
}

int wavenums=nUpzer o-1;
return wavenums;
Mixed code and declarations is a C99 addition. I think 'return
nUpzero - 1;' preferable anyway.
}

//define a strcut to record the H and T
struct waveHT
{
//H_max
double waveheight_max;
//H_1/10
double waveheight_1_10 ;
//H_1/3
double waveheight_1_3;
//H_average
double waveheight_aver age;
//T_max
double waveperiod_max;
//T_1/10
double waveperiod_1_10 ;
//T_1/3
double waveperiod_1_3;
//T_average
double waveperiod_aver age;
};
//Function to calculate wave height and period
struct waveHT cal_h_t(double *h,size_t n)
{
struct waveHT wave_h_t;
//H_max
double *tmp_hmax;
tmp_hmax=(doubl e*)malloc(sizeo f(double));
wave_h_t.wavehe ight_max=gsl_so rt_largest(tmp_ hmax,1,h,1,n);
wave_h_t.wavehe ight_max=*tmp_h max;
free(tmp_hmax);
You don't *need* malloc just because gsl_sort_larges t needs a pointer:

double tmp_hmax;
wave_h_t.wavehe ight_max = gsl_sort_larges t(&tmp_hmax, 1, h, 1, n);
>
//H_1/10 or H_s
double *tmp_hs;
tmp_hs=(double* )malloc(n/3*sizeof(double ));

free(tmp_hs);
???
>

return wave_h_t;
}
int main(void)
{
int i,j,row=10240,c olumn=3;
int numWaves;
int indexH[row/2];
Here I would use a pointer and malloc a guessed size.
double a[row];
double dt=0.02;

struct waveHT htest;

for(i=0;i<row;+ +i)
{
a[i]=sin(-0.2+i*dt);
}

// numWaves=nUpzer o(a,row);
numWaves=upzero H(indexH,a,row) ;
and then pass the size along with a pointer to indexH so upzeroH can
re-allocate the array is if needs to (and change indexH to point to
this new bigger array).

You can avoid passing a 'double **' if you have upzerH return a poiner
to the new, possibly larger, array. I would opt for the 'double **'
route on balance.

<snip(I did not look at all the code.)

--
Ben.
Oct 30 '07 #2
"Bowlderste r" <bo*********@gm ail.coma écrit dans le message de news:
87************@ gmail.com...
Hi,all.

I produce a function to analysis the test data,which is wave signal, and
stored as array a[i].
I want to figure out how many times it upcrosses zero,which means that
when a[i]<0,and a[i+1]>0,
it upcrosses zero one time.I need store the numbers of upcross zero, and
the index where it upcrosses
zero, for example, index i(a[i}<0&&a[i+1]>0) and index
j(a[j]<0&&a[j+1]>0).Between a[i] and a[j],
there is no upcross zero signal. The maximum and minimum value between
a[i+1] and a[j] need to be found,
and a new array is defined as h[n], which is used to store MAX-MIN between
a[i+1] and a[j].So the size of
array h should be NUM-1, where NUM is the numbers of upcross zero times.

But now I can not define the array h with the size NUM-1.

The following is my function, please note the pointer indexH in function
upzeroH, and the array indexH in
the main function, where I define it with size large enough as row/2.I
want it's size just is wavenums.

Would you please give me a hand? Any suggestion will be helpful.

Thank you for your attention.

Bowlderster.
<snip packed cobs of code>

What is wrong with your space bar ?
Your code is unreadable.
I would grade it as F without further study.

--
Chqrlie.
Oct 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3141
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x"; char search = '?'; char* replace = "***"; char* result = search_and_replace(source,search,replace);
4
1613
by: S Kemplay | last post by:
Hi all, I am after any pointers you may have to improve these two functions. They both work but are part of an assignment - I am looking for feedback. The first one reads records from a file - the teacher won't allow a delimeted file he wants spaces between words joined with an underscore. The records are read into an array of structs:
9
4619
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
23
1841
by: philipl | last post by:
hi, I have some code here which basically look for within a string, the occurance of any 3 consectative characters which are the same. so AAA bbb etc would be reported by this function. I later added some code which is needed to detect the scenario where the same char appears 4 times for example AAAA, in this case want to skip the remaining 3 chars and check the next character in the string. This has led me to increase my for loop...
4
1171
by: Trint Smith | last post by:
How can I improve this code please? It sometimes produces this error: "Object reference not set to an instance of an object" When I do this: strSQL = "UPDATE TBL_Items SET" & _ " item_itemnumber = " & PrepareStr(Label6.Text) & _ " ,item_itemcurrentbidprice = " & PrepareStr(itemCurrentbidprice) & _ " ,item_itembidhistory = " & PrepareStr(itembidHistory) & _
41
2538
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would like to ask if the principles used in this code are still valid in the "modern" Python and if/how it can be improved (revrited) using futures of current version of...
2
1281
by: HerbF | last post by:
Is there a simpler, more elegant way to specify what is shown and what is hidden than the following primitive script? <script type="text/javascript"> var pN = new Array(); pN="Text 1."; pN="Text 2"; pN="Text 3"; pN=""Text 4"; pN="Text 5";
9
1584
by: goosen_cug | last post by:
This program is a "Sequential List" class I want to do the Union Operation,Intersection Operation of the Set.But this program have a problem: /////////////////////////// Compiling... Set.cpp H:\cheung\Set\Set.cpp(81) : error C2664: 'Insert' : cannot convert parameter 1 from 'int' to 'int &' A reference that is not to 'const' cannot be bound to a non-lvalue
75
4214
by: At_sea_with_C | last post by:
Hello all, I have written an ascending sort routine for floats. This seems to do the job, but for elements over 10,000, it gets awfully slow. A lot of useless comparisions with previously sorted elements are made. I was thinking of using a function, that kept an index, to selectively iterate over unsorted elements. But I am unable to think of a way to do it. Any ideas to improve the routine and get a better grade? Thanks to all.
11
2141
by: Vyas111111 | last post by:
Hello all I have a windows form,in this page I am creating lots of controls at runtime.So when my form is load it takes time to load all controls.How can i improve performance of that page.I have Call the InitializeComponet() function in constructor of the class.
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.