0

I have a signal, and I want to add a plateau (Flattening) in my signal where ever I find local maxima.

I have provided an example here to make my question easier to understand as the size of my real data is 1x1666520, so I cannot upload the whole file here.

What I want to do and what I am expecting:

• I want to make my signal flat where we will find local maxima in signal.

• I want to apply window to the signal to visualize the effect of flattening in a certain range and also to visualize my signal more deeply and clearly. I also want to control the size and width of window.

Here is a sketch of how the output should look

Sketch with flattened local maxima

I have local maximas at points 5, 1, 7, 8, 4.

When I am at a local maxima, I want my signal to get flat (clip) and become a straight line around that point.

I also want to control the width of flattening. Let’s suppose width is 0.2 as I have mentioned in hand sketch. If so, I want to make it 0.3, so I also need to control width of clipped part.

Updated Details with more clarity:

What I am Doing:

I have 2 signals. 1st Reference and 2nd Measured Signal from Sensor. (I have taken 6 different measurement signals from 1 sensor).

The measured signals have delay, offset, flattening in it.

I want to make my reference signal to look alike my measured signal by putting delay, offset and flattening in signal to fulfill my needs.

To do flattening in my original signal I have observed that at every local maxima I have got flattening in signal and in every measurement flattening it is different.

So I want to make my signal flat at every local maxima.

Y axis: Height of flattening (I want to control the height of flattening at y axis)

Means I want to make a loop which contains varying values of flattening at y axis e.g In first attempt I want to give flattening of 0.1 then in 2nd attempt want to give flattening to al local maximas of 0.2 and observe their effect. In short when I have local maxima so I want to give a flattening of (locs – 0.1) in the maxima on y axis.

Means if I am getting peak at 5 so I want to give flattening at (5-0.1) at 4.9. Then in 2nd loop want to check the effect of (locs-0.2) in the local maxima on y-axis.

Want to apply loop for values from 0.1 to 0.9.

X-axis: Width of flattening (I want to change the width of flattening)

Means I want to observe the effect of change in width of flattening by giving a range of values. This can be done by the help of loop.

In short when I have local maxima so I want to give a flattening width of 0.1 in the maxima on x axis.

Then in 2nd loop want to check the effect of 0.2 width change in the local maxima on x-axis.

enter image description here

Original Data Details :

In my original data x axis contains values in decimals so don’t want to interpolate data.

In my original data y axis also contains values in decimals.

size of my real data is 1x1666520.

Original Signal looks like

enter image description here

Window effect:

The window is just to observe the behavior of change occurred at place of local maxima means like if width of flattening is 0.1 so window is about 0.2.

I am sorry I remained unable to provide a good example data that explains exactly my situation in MATLAB so I have attached a handmade sketch. I hope that I remained capable enough to make my question clear.

Code:

t = 1:25 ; 
A = [1 0 1 2 3 5 0 1 0 0 0 2 3 6 7 0 0 8 0 1 1 2 3 4 2];
[pks,locs] = findpeaks(A)

win1 = hamming(numel(A))';
xw1 = win1.*A; 

figure 
plot(t,xw1,'r',t,A,'b')

Modified Code on suggestion of Jon for flattening

Jon gave an idea to consider values before and after maxima and make them same as of maxima to make the width controllable.

 t = 1:25 ; 
 A = [1 0 1 2 3 5 0 1 0 0 0 2 3 6 7 0 0 8 0 1 1 2 3 4 2];
 [pks,locs] = findpeaks(A)
 % A(A>locs)=locs
 figure 
 plot(t,A,'b')
 A(locs+1) = A(locs);
 A(locs-1) = A(locs);
 hold all;plot(t,A,'r');

enter image description here

  • Updated Question:

Please have a look at the attached figures of Original signal and quantization steps in signal. Actually I am sorry I remained unable to upload the whole data file.

enter image description here Actually my original Signal contains decimal points with step size of 0.001. So my signal is sampled and quantized.

By doing Interpolation it is working perfectly in making flattening to the signal but unfortunately it is removing the quantization of my original signal and also Interpolation is making the process very slow.

Is there any other way to clip the signal at the point of local maxima?

I will be really grateful to you for your help because I am clueless at this stage.

I have tried and tested the code provided by john. It is working great but issue is with my Data. IT contains quantized steps.

Important Points:

  1. My Original Data is in Steps or Quantized.
  2. It contains decimal points with size of 0.001 so by doing interpolation the points on x-axis and y-axis .So my data and time contains points with difference of 0.001.
  3. By Doing Interpolation it is working Perfect but problem is that it is removing quantization of signal.

I will be really grateful to you if somebody help me with it.

Request:

If you have some questions please ask so I will be able to make my query better.

Peter
  • 161
  • 1
  • 11
  • 2
    How do you want to flatten it? Because the first obvious answer is `A(A>mymax)=mymax` – Ander Biguri Aug 17 '17 at 13:49
  • Thanks a lot Ander Birgui . So in my case for the above example I am getting pks at 5,7,4 means local maximas so now What I want is to make my signal flat means straight line at local maximas 5,7,4 upto certain value means my signal gets flat for 2 points in x-axis when it sees the local maxima. Please have a look at the attached diagram. I hope that it will clarify my point and this is the reason I also want to apply window to signal so that I can visualize effect of flattening clearly. – Peter Aug 17 '17 at 14:00
  • In the attached picture above Green signal shows effect of flattening and this is what i want to do with my signal – Peter Aug 17 '17 at 14:00
  • 1
    So..... literally what I said, right? – Ander Biguri Aug 17 '17 at 14:02
  • Yes I think so but would not u think that it should be greater than and equal to means if A = 5 and it matches my local maxima means 5 so it will flat my signal upto 2 points A(A>=pks)=2 . Is it right because I am confused in it that how to do it. – Peter Aug 17 '17 at 14:06
  • Please just only consider green signal so if I get local maxima i should clip my signal upto a value of 2 .. Clipping is a good word to get some more clarity. – Peter Aug 17 '17 at 14:09
  • 1
    Mate, read the line I gave you. just do `mymax=5;` before it, and it just works. Just choose what value is the maximum one, and all values that are hihger than the maximum, change them to the maximum. – Ander Biguri Aug 17 '17 at 14:10
  • I have edited as par ur recommendation can u have a look at where is the mistake please. Also Thanks for ur time and also regarding Window selection and implementation can u suggest some information . – Peter Aug 17 '17 at 14:19
  • 1
    Your mistake is either 1) Not listening to what I said 2) not explaining what you want. I suggest you try to fix 1) first, then perhaps 2). – Ander Biguri Aug 17 '17 at 14:20
  • @Peter: When you realize exactly what you want, please update the *question* to show the changes. In its current, I've voted to close this question as "unclear what you're asking". Future Stack Overflow searchers should not have to read comments on an answer to understand your question. – Prune Aug 17 '17 at 16:32
  • 1
    @Peter What are you showing with your most recent edit? Is this a working solution? Then you should write an answer. If it is not a working solution, you need to tell us what needs to be improved. Just adding code is doesn't tell me what you need. – Cecilia Aug 18 '17 at 20:38
  • @Peter I've tried to make your question easier to understand by moving some of your information around. – Cecilia Aug 18 '17 at 20:50
  • 1
    Cecilia Thanks a lot for ur goodwill gesture and ur time – Peter Aug 20 '17 at 09:16

1 Answers1

2

Not sure what you're looking for, especially with the windowing. For the flattening to "2 points in x-axis" maybe make the local max the same as the previous value?

Continuing from your code:

figure 
plot(t,A,'b')
A(locs) = A(locs-1);
hold all;plot(t,A,'r');

enter image description here


EDIT Whole code:

t = 1:25 ;
A = [1 0 1 2 3 5 0 1 0 0 0 2 3 6 7 0 0 8 0 1 1 2 3 4 2];
t2=1:0.1:25;
A = interp1(t,A,t2); %re-make example data to have decimal points on the x-axis
t=t2;
[pks,locs] = findpeaks(A);
figure
plot(t,A,'b')

windowWidth_x2 = 2; %define how wide, this is 0.2, because the x-axis increments by 0.1

valuesToClipTo = A(locs-windowWidth_x2); %No idea what you actually want
for ii=1:length(locs) %Explaining is futile
    A((locs(ii)-windowWidth_x2):((locs(ii)-windowWidth_x2)-1+find(A((locs(ii)-windowWidth_x2):end)<valuesToClipTo(ii),1,'first'))) = valuesToClipTo(ii);
end
hold all;plot(t,A,'r');

  • Jon this is what I am looking for flattening part exactly what I want to do. Thanks a lot for ur time and help regarding it and regarding windowing I just need to apply window on signal let’s take an example I need to apply window of size(5) on my signal and want to visualize my signal in that window duration and that’s what I tried to do just applied tried to apply window to visualize the signal that I have made flat. – Peter Aug 17 '17 at 14:43
  • Jon 1 more thing sorry i visualized it now This logic is great but if I want to just step down one value below and make it flat means in this case before local maxima 8 I have value of 0 so it pulls down my graph to 0 but I want it to make signal flat means a straight line at 8 ,a straight line at 5,a straight line at 4 etc. . Sorry I visualized it deeply so I came to know this issue. Is there some logic for this? – Peter Aug 17 '17 at 15:25
  • 1
    Not sure what you mean, since I'm not sure I know your goal. Maybe make the next value the same as the local max? `A(locs+1) = A(locs);` –  Aug 17 '17 at 16:17
  • Jon please have a look at the updated question as u came up with some good solutions may be u will find exact . Thanks. – Peter Aug 17 '17 at 18:23
  • A(locs+1) = A(locs); A(locs-1) = A(locs); – Peter Aug 18 '17 at 09:03
  • I have also tried these and these are working good but I also want to control the width of flattening so my be u provide some suggestions for that how to control width of flattening of signal. – Peter Aug 18 '17 at 09:04
  • because in this way i can only produce flattening based on previous and next value that is good as half of issue can be solved this way. – Peter Aug 18 '17 at 09:05
  • Or if I want to include values of 2 values of locs means like 0 1 3 1 1and local maxima is 3 and and if I want to make values close to 3 all same like 3 3 3 3 3 how can it be possible as I have tried A(locs+2) = A(locs); A(locs-2) = A(locs); but its not working as it only makes value same which is located at 2nd point . means 3 1 3 1 3 – Peter Aug 18 '17 at 14:36
  • Peter, my edit was a shot in the dark: Your edit is hard to understand since: 1) there are no decimals in the x-values of the graph - are decimals in your original data? 2) Your edited code does the opposite of clipping, since you changed it based on the comment, "pulls down my graph to 0 ". Sorry, I can't help you without having a clearer goal. Maybe update: Why you are clipping, or what do you want the y-value to be after clipping? –  Aug 19 '17 at 19:36
  • Thanks a lot Jon for such an elaborated and useful response today i will update my question with more clarity sorry for not being able to send a quick response. – Peter Aug 20 '17 at 08:42
  • Jon thanks a lot for ur time and great efforts . Now I have updated the qurrey with all details what I actually want . I hope i will remain successful this time to make myself able to explain it .Unfortunately remained unable to provide example data so provided handsketch of what i want. – Peter Aug 20 '17 at 15:07
  • Jon also thanks a lot for understanding my qurrey quite well though there were some loop holes from myside but i have updated those I hope that it will work . – Peter Aug 20 '17 at 16:39
  • Yeah, I think the code above will work, just make two for loops around the current one, and replace `windowWidth_x2` and `valuesToClipTo` with the values you want. It may take some trial and error. Then just figure out the windowing. You can update when you've tried. –  Aug 21 '17 at 13:38