|
Sorry, I had no idea I had double posted. Possible lag at my end.
Thanks for the head up.
|
|
|
|
|
Ah, I see. That wasn't quite what your initial post described. So, effectively, you're pinning a value and as long as the next value continues the trend of moving upwards (as an example), we keep going until we hit the maximum. As soon as the value moves in the opposite direction, we repin the value we're comparing against. Does that sound right to you?
This space for rent
|
|
|
|
|
Hi Pete,
That is almost correct. If the value changes direction from say north to south but the distance south is < 6 from the pinned max then the trend should remain northerly until the change south from the pinned value is >= 6.
E.g. south 5 north 3 south 5 constitutes a southerly trend and north 5 south 3 north 5 constitutes a positive trend even though the direction is changing. The trend only shifts in the opposite direction once we get to pinned <=6.
Hope that's clearer
Best Regards
Nigel
|
|
|
|
|
Sounds like you just need to loop through the array, doing a single check for each item?
Member 12616185 wrote: The following data represents a typical trace. Only points 9,11, and 24 are real points that should be returnd. Hehe, so you are requesting code?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
I have code that returns the peaks and troughs but not the window/peak excursion implementation. I was hoping there would be an elegant way to do this in maybe linq.
I posted my current code in a previous reply. It returns the peaks and troughs.
I could post process this in a bunch of loops but it was getting messy so I decided to request help from the Brain, AKA codeproject c# forum
Kind Regards
Nigel
|
|
|
|
|
Converting things to LINQ does not always make them more "elegant". Keep it readable and simple
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Your spot on with that assessment LINQ isn't everything, and by elegant I meant not done with a 100 for/if loops etc. If it can be done in LINQ then great . I'm just blowing my brain trying to conceptualise the best way of doing this in code.
Br
Nigel
|
|
|
|
|
Nigel_Davison wrote: LINQ isn't everything, and by elegant I meant not done with a 100 for/if loops etc. LINQ will do the same, just in another form of notation.
If you need to inspect a list of values, a loop will be somewhere. Also doesn't look like you have that much loops/branches yet. If it works, it works
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hey Eddy
>> "Also doesn't look like you have that much loops/branches yet"
Yes, but the code I posted only retrieves the peak maxima/minima. It doesn't respect the 6dB peak excursion requirement.
>> "If it works, it works"
Couldn't have put it better myself
Br
Nigel
|
|
|
|
|
Krellon wrote: It doesn't respect the 6dB peak excursion requirement. Did you mean "exclusion"?
Given the list you return, which (simple) steps should I do on paper to satisfy your requirement? I was assuming you'd just check the previous and following values and add them to a list of the delta is bigger than 6 doorbells.
The simpeler the explanation, the simpeler the code (usually)
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
If you stick the following data into Excel and plot it. Then place markers on the 9th 11th and 24th point you would see what I was after.
double[] trace = new double[] { 47.92370605, 44, 49, 46.31715393, 44.98129654, 46.17653275, 78, 73, 82, 63, 69, 41.72979736, 41.13000488, 41.49586487, 37, 40, 30, 40.79894257, 40.65593719, 41.37298584, 39.13498688, 55, 51, 60, 39.3440094, 38.51491547, 42, 35.12589264 };
If I could embed an image into my comment I would show you.
Your essentially looping through an array of points looking for the biggest and smallest peaks and waiting for the delta either side them to go >= 6dB. When it does you grab the biggest peak between the -6dB points. Rinse and repeat.
So,
1,6,3,7,1 would yield a peak at p[4] = 7 with a window size of 5.
1,7,1 would yield a peak at p[2] = 7 with a window size of 3.
1,-3,-2,7,5,1 would yield a peak at p[4] = 7 with a window size of 4.
1,-7,1,-6 would yield 2 peaks. One at p[1] = 1 with a window size if 2 and one at p[3] with a window size of 3.
stick them all together and you get
1,6,3,7,1,1,7,1,1,-3,-2,7,5,1,1,-7,1,-6
Peaks at p[4], p[7], p[12] and p[17]
Respective window sizes in points would be, 5, 3, 4 and 3
Hope that's helps
Br
Nigel
|
|
|
|
|
Krellon wrote:
If I could embed an image into my comment I would show you. A good thing you can't; I asked not only because I'm bad at math, but also because I'm a bit simple. The PC is too, so once you can describe your problems in those kind of steps it becomes easy to translate it to code.
So I opened Calc, generated a chart, and.. you're looking for those three tops?
Krellon wrote:
Your essentially looping through an array of points looking for the biggest and smallest peaks and waiting for the delta either side them to go >= 6dB. When it does you grab the biggest peak between the -6dB points. Rinse and repeat. That sounds like pseudo-code, so let me pseudo away (without compiler)
double lastValue = 44;
List<double> peaks = new List<double>();
for (int i = 0; i < trace.Length; i++)
{
double currentValue = trace[i];
double delta = currentValue - lastValue;
if (Math.Abs(delta) > 6)
{
DoMagic();
}
lastValue = currentValue;
}
There's a DoMagic in there, because that's a complex sentence there. From which range would you want the maximum? Which two points would the -6dB points be? Do you want to use a fixed window size?
If you can describe that in simple terms, you can complete the code
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
That's pretty much it.
Two things to note though are, inside so magic we check the peak is a positive peak and not a trough with 6dB sides.
and
lastvalue would become the index of the point >= 6db down on the right side of the peaks slope.
What this means is that the window could grow dynamically. By setting a physical window width we can limit the max search window +/- either sode of the located peaks.
Thanks again for your time
Br
Nigel
|
|
|
|
|
At any point, did you realize that you are talking in math and that I did not understand a word?
Krellon wrote: What this means is that the window could grow dynamically. By setting a physical window width we can limit the max search window +/- either sode of the located peaks. For that, you want recursion. This can be(come) as simple or as complicated as you like
Krellon wrote: Thanks again for your time You're welcome, ofc. If you want to pay it back, simply answer someone elses question in return
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
using System;
namespace ConsoleApplication10 {
class Program {
static void Main( string[] args ) {
double[] trace = new double[] {
47.92370605, 44, 49, 46.31715393, 44.98129654, 46.17653275,
78, 73, 82, 63, 69, 41.72979736, 41.13000488, 41.49586487,
37, 40, 30, 40.79894257, 40.65593719, 41.37298584, 39.13498688,
55, 51, 60, 39.3440094, 38.51491547, 42, 35.12589264 };
double low = trace[ 0 ];
double previous = trace[ 0 ];
double peak = trace[ 0 ];
for ( int i = 1; i < trace.Length; i++ ) {
double current = trace[ i ];
if ( current > previous ) {
peak = current;
} else {
if ( current < previous ) {
if ( ( peak - low >= 6.0 ) && ( peak - current >= 6.0 ) ) {
Console.WriteLine( $"Peak: {peak} @{i}" );
low = peak = current;
}
}
}
previous = current;
}
Console.ReadKey();
}
}
}
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi Gerry,
This is a smashing bit of code and works great on the sample data provided. However, if you change p10 value 63 to 64, we lose peak 11 value 69 as expected but we also lose peak 24 at level 60.
Many thanks for the great code it gives me food for thought
Nigel
|
|
|
|
|
One works with the samples provided.
Your "requirements" keep changing.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
It's not as much as a question, but more a set of specs. My interest in the question is gone suddenly
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
New here. New to coding.
I am looking to make a program which causes a picture to pop up (perhaps in a WindowsForm) when a certain word ("dog", for instance) is typed by the end user.
So, say someone is writing a story in Word and they type the word "dog" as part of their story. How could I make a picture of a dog pop up for a few seconds every time the word is typed?
I think it has something to do with KeyPressEvenHandler, but I have tried to find many sources for this, and each time I find an example, there are about 20 errors, even when I follow the directions to the letter.
It is very frustration, but I will figure it out, hopefully with someone's help here.
Thanks in advance for any help you can provide. Also, If someone has time to give me some code to get started that will actually run, that would be very helpful. Only been studying C# for about a month, and I learn best from example.
|
|
|
|
|
This is far more complex than you realize.
The events you see in the form are useless to you because your form won't have the input focus if the user is typing in Word.
To do something like that requires the use of a global keyboard hook. This is a rather advanced topic, not for the newcomer to programming!! But, if you want to see examples, just Google for "C# global keyboard hook".
|
|
|
|
|
On the contrary, I had no doubt that it was difficult. And I also realize that it is a very ambitious attempt for a noob. But your suggestion for Google is precisely what I needed. Thank you very much for taking the time to answer. I was having a hard time searching for the keywords that would lead me to the answer. I'll educate myself in due time.
Meanwhile, if anyone else can offer an example, that would be cool, too.
Thanks again, Mr. Kreskowiak!
|
|
|
|
|
Big picture: if you are "New to coding," I think pursuing something this complex ... inter-op with Word ... is not a good strategy to maximize what you learn. Why not take one of the many extended Rich TextBoxes for WinForms, and use that for your editing control ?
To tango with Word, you need to research, and learn to use, inter-operation between MS Word and C#. Lots of resources on this: search here, and on the net [^]
Then, you need to determine what are the possibilities for intercepting Key Events in MS Word from C#: can you install/hook your own KeyDown handler from C# into the running instance of MS Word ? , [^], For all I know, this may require writing a Word add-in.
If you get that far, then you have to implement your own equivalent to an auto-completion facility: that's not hard: if you ask a specific question on that, I will respond with code.
If you can't get MS Word key events in C# as described above: then you are going to need some form of hook, as Dave K. mentions.
If you do get a hook working, then every keystroke in Word is going to require a call though the hook to your code: I suspect that will have a real impact on performance, depending on your hardware, memory, etc.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Thank for such a detailed reply. It should certainly set me in the right direction.
|
|
|
|
|
As a novice, I would strongly suggest that you "shelve" this project for a while, and get some solid experience with "normal" C# before you think about considering this.
Global C# hooks are not trivial at all, and a very small mistake in your code can seriously destabilize your whole system: they have to "hook" directly into low level stuff and bypass the "usual protection" the OS provides to prevent one app from affecting another. I would recommend good, solid, regular, air-gapped backups before each attempt to do this: you could bugger your system to the point of reformat and reinstall very, very easily...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If you create a "context / scenario", it may be easier to move forward.
If you start with a "large" textbox, you can obviously type in some "words".
The "textbox" allows you to determine "where" the cursor is in the textbox, if a "word" is selected, and so forth.
c# - How to get current word at the Caret position from a String(TextBox) which is having SPACE and ENTER key? - Stack Overflow
"Keyboard hooks" require you to practically create your own "word processor"; when all you want to do is "parse some text".
Google "text handling"/ textboxes instead of "hooks".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|