Good point!
Krellon
Posts
-
change image every day c# Linq -
change image every day c# LinqMake sure you have enough images in the images folder :P
List images = new List(); for (int i = 0; i < DateTime.DaysInMonth(2018, 2); i++) { images.Add(Image.FromFile(@"\\IMG" + i + ".jpg")); } var currentImage = images\[DateTime.Now.Day\];
-
Peak Search Algorithm with sliding window and peak excursionHi 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
-
Peak Search Algorithm with sliding window and peak excursionHi 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
-
Peak Search Algorithm with sliding window and peak excursionHi 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
-
Peak Search Algorithm with sliding window and peak excursionSorry, I had no idea I had double posted. Possible lag at my end. Thanks for the head up.
-
Peak Search Algorithm with sliding window and peak excursionHi Pete, Many thanks for looking into this for me. This is a great attempt but its using a sliding window observing the points immediately adjacent the current point. If you change data point p8. from 73 to 77, you would observe that the peak at p9. level 82 is no longer detected. p9. is still a legitimate peak as it is 6dB above p6 and p10. The peak would have changed from being 3 points wide to 5 points wide. Think of a peak as an noisy parabola which could be represented with >= 3 points. If the peak to troughs are less than 6dB then they all form part of the same emissions. The seperation points being when the emissions lobes descend to >= -6dB. Hope this helps and thanks again for your help.! Br Nigel
-
Peak Search Algorithm with sliding window and peak excursionHi Pete, Many thanks for looking into this for me. This is a great attempt but its using a sliding window observing the points immediately adjacent the current point. If you change data point p8. from 73 to 77, you would observe that the peak at p9. level 82 is no longer detected. p9. is still a legitimate peak as it is 6dB above p6 and p10. The peak would have changed from being 3 points wide to 5 points wide. Think of a peak as an noisy parabola which could be represented with >= 3 points. If the peak to troughs are less than 6dB then they all form part of the same emissions. The seperation points being when the emissions lobes descend to >= -6dB. Hope this helps and thanks again for your help.! Br Nigel
-
Peak Search Algorithm with sliding window and peak excursionHi 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
-
Peak Search Algorithm with sliding window and peak excursionHey 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
-
Peak Search Algorithm with sliding window and peak excursionYour 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
-
Peak Search Algorithm with sliding window and peak excursionHi 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
-
Peak Search Algorithm with sliding window and peak excursionHi Pete, Thanks for getting back to me, I have a modified peak search function that returns peak/troughs.
public static IEnumerable> LocalPeakMinMax(IEnumerable source, int windowSize)
{
if (windowSize < 2) windowSize = 2;windowSize = windowSize - windowSize % 2 + 1; int halfWindow = windowSize / 2; int index = 0; var before = new Queue(Enumerable.Repeat(double.NegativeInfinity, halfWindow)); var after = new Queue(source.Take(halfWindow + 1)); foreach (double d in source.Skip(halfWindow + 1).Concat(Enumerable.Repeat(double.NegativeInfinity, halfWindow + 1))) { double curVal = after.Dequeue(); if (before.All(x => curVal > x ) && after.All(x => curVal >= x )) { yield return Tuple.Create(index, curVal); } else if (before.All(x => curVal <= x) && after.All(x => curVal < x)) { yield return Tuple.Create(index, curVal); } before.Dequeue(); before.Enqueue(curVal); after.Enqueue(d); index++; } }
The problem is I cannot figure out how best to filter out all the chaff and return the peaks that align with the prescribed logic explained in my original post. I was going to take the output from this and play with it but it was getting messy. I was hoping the community may be able to come up with a more elegant solution. The code isn't slow at all, quite the opposite. At least with the 625 points i'm currently playing with. Br Nigel
-
Peak Search Algorithm with sliding window and peak excursionDear coder/Mathematician folk, I have a question on how to best peak search a series of noise like points. The points are stored as doubles in a double array. The points represent voltage levels in dBuV. I'm not a mathematician and I have limited knowledge regarding the elegancie's of how C# can be put to handle complex problems. Or maybe not so complex to some :) I would like 'n' points returning that have a delta from there neighbours of >= 6dB and within a specified window of 'm' points. The window allows us to reject all peaks regardless of delta bar the point with the highest peak. As we go through the data points we look at each point [DP] with respect to its neighbour. If the delta to left neighbour [P1] is >= 6dB and delta to right neighbour [P2] is >= 6dB we have a peak. if either neighbours delta is less than 6dB we may have a slope condition. We must there for ride the slope with respect to [P2] goint up and down the troughs until we hit a transition and the delta from [P2] is >= 6dB. This would be our next point. We repeat the process though all points returning just the peaks that are separated by the window width and have clear >= 6dB margin from there neighbours. The following data represents a typical trace. Only points 9,11, and 24 are real points that should be returnd.
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};
I hope I explained things clearly. Please feel free to ask for any clarifications :) Many thanks for your time and effort Nigel
-
lambda expression to retrieve sequence based on an array value where the array is stored in the sequenceHi Homer, I thought the query might return all results in the collection where value == 802.11g and Object2 wasn't equal to null. I have so much to learn :) I did a test run and you were right! all objects came back in the collection. I have since implemented your recommendation and that worked :) Here's my test code for ref.
using System.Collections.Generic;
using System.Linq;namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
List origCollection = new List()
{
new Object1() {ID = 1, theList = new List()
{
new Object2() { Name = "Mode", Value = "802.11b" },
new Object2() { Name = "BW", Value = "20" },
new Object2() { Name = "Chan", Value = "1" },
new Object2() { Name = "Tech", Value = "SISO" }
}
},
new Object1() { ID = 2, theList = new List()
{
new Object2(){ Name = "Mode", Value = "802.11g" },
new Object2(){ Name = "BW", Value = "20" },
new Object2(){ Name = "Chan", Value = "1" },
new Object2(){ Name = "Tech", Value = "SISO" }
}
}
};var theCollection0 = origCollection.Where(m => m.theList.Select(n => n.Value == "802.11b") != null); // Broken var theCollection1 = origCollection.Where(m => m.theList.Any(n => n.Value == "802.11b")); // Works! var theCollection3 = origCollection.Where(m => m.theList.Any(n => n.Value == "802.11z")); // Returns Empty as expected. }
};
class Object1
{
public int ID { get; set; }
public List theList;
}class Object2
{
public string Name { get; set; }
public string Value { get; set; }
}
}Many thanks for spotting and fixing this! your a star :) Br Nigel
-
lambda expression to retrieve sequence based on an array value where the array is stored in the sequenceThanks Member 12616185, Yes, Object1 was meant to be a collection of Object1's. My bad for not proof reading. Thanks for spotting and correcting. Your reply worked a treat. Here's the implementation I used which served my purpose.
var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);
Many thanks again for the help Br Nigel
-
lambda expression to retrieve sequence based on an array value where the array is stored in the sequenceHi guys, Probably a simple task but i'm struggling with it. I have a list of objects, lets call this a list of Object1. Each Object1 contains a list of another object lets say Object2. Object2 has 2 properties. 'Name' and 'Value'. I want to be able to select Object1 where Object2.Name equals a specified value using lambda. eg
class Object2
{
public string Name {get; set;}
public string Value {get; set;}
}class Object1
{
public List theList = new List(){ new Object2(){Name = "Blah", Value = "Blah"}};
}public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();var myList = Object1.Where(m => m.theList.Name == "Blah") // This is where im confused and would like some clarity of operation }
}
I hope I explained myself clearly. Many thanks for your help Br Nigel