Smooth noisy data whilst keep absolute minimum and maximum values
0 Comments
Answers (3)
0 Comments
0 Comments
Hi @Bill White ,
You have to understand the challenging part when you apply smoothing techniques like sgolayfilt, the algorithm works by fitting a polynomial to a specified window of data points. This process can lead to a shift in the local minima, as the polynomial fit may not respect the original data's constraints, particularly if those constraints are not explicitly defined in the smoothing process. So, here are some possible solutions to help you out.
Post-Smoothing Adjustment: After applying the smoothing function, you can adjust the smoothed signal to ensure that the local minima are set to zero. This can be done by identifying the local minima and then shifting the entire signal downwards by the minimum value.
Custom Smoothing Function: Instead of using sgolayfilt, you could create a custom smoothing function that incorporates constraints for the minima. This would involve more complex programming but could yield better results tailored to your specific needs.
Using islocalmin: As this function is suggested by @Star Struder, it can be employed to identify the local minima in your smoothed signal. Once identified, you can adjust the signal accordingly.
Below is an example code snippet that demonstrates how to smooth a noisy sinusoidal signal while preserving the local minima using the sgolayfilt function and the islocalmin function.
% Generate a noisy sinusoidal signal t = linspace(0, 10, 1000); % Time vector signal = sin(t) + 0.1 * randn(size(t)); % Noisy signal
% Apply Savitzky-Golay filter frameLength = 901; % Frame length polynomialOrder = 2; % Polynomial order smoothedSignal = sgolayfilt(signal, polynomialOrder, frameLength);
% Identify local minima in the smoothed signal TF = islocalmin(smoothedSignal); % Logical array of local minima localMinima = smoothedSignal(TF); % Values of local minima
% Adjust the smoothed signal to ensure minima are zero minValue = min(localMinima); % Find the minimum value if minValue < 0 adjustedSignal = smoothedSignal - minValue; % Shift up to make minima zero else adjustedSignal = smoothedSignal; % No adjustment needed end
% Plotting the results figure; plot(t, signal, 'b', 'DisplayName', 'Noisy Signal'); hold on; plot(t, smoothedSignal, 'r', 'DisplayName', 'Smoothed Signal'); plot(t, adjustedSignal, 'g', 'DisplayName', 'Adjusted Signal'); legend show; xlabel('Time'); ylabel('Amplitude'); title('Smoothing a Noisy Signal While Preserving Local Minima'); grid on;
So, as you can see in above code snippet, a noisy sinusoidal signal is created using sin and random noise. Then, sgolayfilt function is applied to smooth the signal. Afterwards, islocalmin function identifies the local minima in the smoothed signal. If the minimum value of the smoothed signal is less than zero, the entire signal is shifted upwards to ensure that the local minima are zero. Finally, original noisy signal, the smoothed signal, and the adjusted signal are plotted for comparison.
Please see attached.
Hope this helps. Please let us know if you have any further questions.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!