Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem that I face is more of the program logic (I think...)
So , I am a physics enginner with some c sharp background (simple math programs, etc).
I have an data aquisition instrument that give me a lot of data that needs to be filtered. I can do it by hand but I taught why not use an program for this...

Before showing the code let me explain you what this program needs to do:

Input:
- input values (double) from my instrument on a multiline textbox with lots(some hundreds) of lines by copy/paste or streamreader from a textfile
- choose by myself a reference value from this textbox lines values.
- input in a textbox an integer that will be an factor.

Output:

The program needs to search in all the values from the textbox - which I converted to a double array,remove from the array all the values that are not in the interval(reference value+factor, reference value-factor),and when it finds in my data the first value that is in the interval (reference value+factor, reference value-factor) use this as a new reference for the removing of data from my array and so on until all the values are processed.

When I run my code (I finished with displaying it on a messagebox just for testing) the program does nothing...no error and no filtering.

C#
string[] unfiltereddata =textBox3.Text.Split(new string[]{ System.Environment.NewLine }, StringSplitOptions.None);
            double reference = double.Parse(textBox1.Text);
            int factor = int.Parse(textBox2.Text);
            double[] unfilterednumbers = new double[]{};
            double d;

            if (unfiltereddata.All(number => Double.TryParse(number, out d)))
            {
                unfilterednumbers = Array.ConvertAll<string,>(unfiltereddata, Convert.ToDouble);
            }

            foreach (var t in unfilterednumbers)
            {
               double difminus = reference - factor;
               double difplus = reference + factor;
               if (t==reference)
               {
                   while (t == difminus || t == difplus)
                   {

                      
                       List<double> filtered = new List<double>();
                       filtered.Add(t);

                       string s = "";
                       foreach (var st in filtered)
                       {
                           s += st.ToString() + "\n"; 
                       }
                       MessageBox.Show(s);

                   } 
               }
                
            }


</double></double>
Posted

1 solution

these lines seem to be your problem:

C#
if (t==reference)
{
    while (t == difminus || t == difplus)
    {


what you are saying there is to display your debug message if the number is exactly equal to 'reference' AND is equal to (diffminus OR difplus) .. this can never be true if factor != 0

also, im not sure the purpose of making the second condition a while loop. if you did manage to hit that peiece of code and have it evaluate to true you would be in an infinite loop as the condition does not get modified

I think what you are after is:

C#
List<double> filtered = new List<double>();
double difminus = reference - factor;
double difplus = reference + factor;

foreach (var t in unfilterednumbers)
{
    
    if (t >= difminus && t <= difplus)
    {
        filtered.Add(t);
    }            
}

string s = "";
foreach (var st in filtered)
{
    s += st.ToString() + "\n"; 
}

MessageBox.Show(s);


Some further tips:

- Once you have the logic sorted, take a look at LINQ, it may well enable you to simplify things further (i didnt use LINQ in this solution as, if you arent familiar with it, it can be a little confusing)
- if you are adding strings together as you are here, a stringbuilder is more efficient
 
Share this answer
 
v2
Comments
barbacot99 7-Sep-11 9:53am    
You're right, I am not familiar with LINQ, I always said that I will learn it and now I'm sorry that I postponed...
The stringbuilder will be implemented, I used messagebox just for testing purpose.
I used the while loop because of this: The problem of removing the values that are not in the interval (reference+factor, reference-factor) is easy. I was thinking that this will be the way of implementing the second condition: after finding the first correct value in the interval (reference+factor, reference-factor) use it as a new reference for the remaining values in the place of the old reference that I picked by myself from the textbox lines.
So I choose the first reference and then the program search my lines and when it finds the first correct value that IS in the interval (oldreference+factor, oldreference-factor) replace this old reference with the new value and continue the processing with (newreference+factor, new reference-factor) untill it finds the first new value in the interval and so on until all the values are processed.
I am really tired of processing by hand the values from this instrument that for 2 hours of data acquisition gives me 50000 of data input and a LOT of it is noise that needs processing. I am in a big hurry so I wrote this piece of code even if i admit that I don't have too much experience in it...

Thank you very much for your kind answer.

Can you also help me with the second condition (after finding the first correct value in the interval (reference+factor, reference-factor) use it as a new reference for the remaining values in the place of the old reference that I picked by myself from the textbox lines.)?
GParkings 7-Sep-11 10:10am    
List<double> filtered = new List<double>();
double difminus = reference - factor;
double difplus = reference + factor;

foreach (var t in unfilterednumbers)
{

if (t >= difminus && t <= difplus)
{
difminus = t - factor;
difplus = t + factor;
filtered.Add(t);
}
}

string s = "";
foreach (var st in filtered)
{
s += st.ToString() + "\n";
}

MessageBox.Show(s);
GParkings 7-Sep-11 10:12am    
Or if you only want to adjust the interval on the first match:

List<double> filtered = new List<double>();
double difminus = reference - factor;
double difplus = reference + factor;
bool firstMatchFound = false;

foreach (var t in unfilterednumbers)
{

if (t >= difminus && t <= difplus)
{
if(!firstMatchFound)
{
difminus = t - factor;
difplus = t + factor;
}

firstMatchFound = true;

filtered.Add(t);
}
}

string s = "";
foreach (var st in filtered)
{
s += st.ToString() + "\n";
}

MessageBox.Show(s);
barbacot99 7-Sep-11 10:50am    
I'm sorry but after so much looking in the monitor for thousands of data and 2 hours of sleep my brains must be fried or something.

Looking and your two solutions and comparing with my problem It would seem that the second is appropriate for me.
So to review: I import the data.
I choose by hand the first value as reference. I choose an factor that will remain fixed all the time.
I want the program to eliminate all the values that are not in the interval (reference+factor, reference-factor) and when it finds the first value that IS in the interval to use this as a new reference and then continue filter all the remaining date with (newreference-factor, newreference+factor) and so on until all the data is processed then for me the second solution works.
Correct me if I am wrong...
GParkings 7-Sep-11 11:00am    
yes, from what you describe, the 2nd solution is what you need.

It might be more clear if i write the pseudocode of what it does anf you can see how that tallies with your requirements:

1. determine lower range of interval from user supplied reference (diffminus)
2. determine upper range of interval from user supplied reference (diffplus)
3. set flag to indicate we havent found a match yet
4. loop through the unfiltered list doing
a. check if the value is inside the range (diffminus to diffplus)
b. if it is check to see if we have found a match before
c. if we havent, adjust the range limits (diffminus and diffplus) according to the unfiltered number we are currently looking at
d. if we found a match add it to the filtered list
5. output the contents of the filtered list

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900