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
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.
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
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');
- 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.
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:
- My Original Data is in Steps or Quantized.
- 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.
- 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.