| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma TextEncoding="UTF-8" |
| | 3 | | #pragma rtFunctionErrors=1 |
| | 4 | | #pragma version=1.10 |
| | 5 | | #pragma ModuleName=IUTF_Utils_Waves |
| | 6 | |
|
| | 7 | | #if (IgorVersion() >= 9.00) |
| | 8 | | static Constant RANDOM_NUMBER_GENERATOR = 3 // Xoshiro256 |
| | 9 | | #else |
| | 10 | | static Constant RANDOM_NUMBER_GENERATOR = 2 // Merseene Twister |
| | 11 | | #endif |
| | 12 | |
|
| | 13 | | /// @brief Search for the first occurrence of the specified dimension label and remove it. If the |
| | 14 | | /// label wasn't found nothing will be changed. |
| | 15 | | /// |
| | 16 | | /// @param wv The wave where the dimension label should be removed |
| | 17 | | /// @param dimension The dimension to search in |
| | 18 | | /// @param label The label to remove |
| 328 | 19 | | static Function RemoveDimLabel(wv, dimension, label) |
| | 20 | | WAVE wv |
| | 21 | | variable dimension |
| | 22 | | string label |
| | 23 | |
|
| 328 | 24 | | variable index = FindDimLabel(wv, dimension, label) |
| 328 | 25 | | if(index != -2) |
| 296 | 26 | | SetDimLabel dimension, index, $"", wv |
| 328 | 27 | | endif |
| 328 | 28 | | End |
| | 29 | |
|
| | 30 | | /// @brief Moves the specified dimension label to the new index and removes it from the old |
| | 31 | | /// position. If the same dimension label is used on more than one place it will only remove the |
| | 32 | | /// first one. |
| | 33 | | /// |
| | 34 | | /// @param wv The wave where the dimension label should be moved |
| | 35 | | /// @param dimension The dimension to search in |
| | 36 | | /// @param label The label to move |
| | 37 | | /// @param newIndex The new index to move the label to |
| | 38 | | /// |
| | 39 | | /// @return The old index of the dimension label. This return -2 if the dimension label was not |
| | 40 | | /// found and -1 if the whole column had the dimension label. |
| 158 | 41 | | static Function MoveDimLabel(wv, dimension, label, newIndex) |
| | 42 | | WAVE wv |
| | 43 | | variable dimension, newIndex |
| | 44 | | string label |
| | 45 | |
|
| 158 | 46 | | variable oldIndex = FindDimLabel(wv, dimension, label) |
| 158 | 47 | | if(oldIndex != -2) |
| 158 | 48 | | SetDimLabel dimension, oldIndex, $"", wv |
| 158 | 49 | | endif |
| 158 | 50 | | SetDimLabel dimension, newIndex, $label, wv |
| | 51 | |
|
| 158 | 52 | | return oldIndex |
| 158 | 53 | | End |
| | 54 | |
|
| | 55 | | /// @brief Shuffles the entries in the wave wv in a random order. It will always use the best PRNGs |
| | 56 | | /// for the current Igor version. |
| | 57 | | /// |
| | 58 | | /// @param wv a 1 dimension text wave that need to be shuffled |
| | 59 | | /// @param startIndex (optional, default 0) The inclusive start index where the shuffle should |
| | 60 | | /// start. |
| | 61 | | /// @param endIndex (optional, default DimSize(wv, UTF_ROW)) The exclusive end index where the |
| | 62 | | /// shuffle should stop. This is usefull for vectors. |
| 0 | 63 | | static Function InPlaceShuffleText1D(wv, [startIndex, endIndex]) |
| | 64 | | WAVE/T wv |
| | 65 | | variable startIndex, endIndex |
| | 66 | |
|
| 0 | 67 | | variable i1, i2, halfRange |
| 0 | 68 | | string tmp |
| | 69 | |
|
| 0 | 70 | | startIndex = ParamIsDefault(startIndex) ? 0 : startIndex |
| 0 | 71 | | endIndex = ParamIsDefault(endIndex) ? DimSize(wv, UTF_ROW) : endIndex |
| | 72 | |
|
| 0 | 73 | | // basic shuffle algorithm |
| 0 | 74 | | for(i1 = startIndex; i1 < endIndex - 1; i1 += 1) |
| 0 | 75 | | // getting second index |
| 0 | 76 | | halfRange = (endIndex - i1) * 0.5 |
| 0 | 77 | | i2 = i1 + floor(halfRange + enoise(halfRange, RANDOM_NUMBER_GENERATOR)) |
| 0 | 78 | | // triangle swap |
| 0 | 79 | | tmp = wv[i1] |
| 0 | 80 | | wv[i1] = wv[i2] |
| 0 | 81 | | wv[i2] = tmp |
| 0 | 82 | | endfor |
| 0 | 83 | | End |