MATLAB: Removing/filtering short periods of a value from a vector

data processingfilteringMATLABsignal processingvectors

Hi, I am processing a signal that has two values, i.e. "on" and "off". In its raw form the signal has lots of "flicker"; short sections of "on" between periods of "off"
example_signal = [1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 1 1 1 1]
I want to filter out the flicker – changing short sequences of less than 4 1s to 0. So the the processed signal would look like this:
processed_signal = [1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
Any help would be very appreciated!

Best Answer

>> V = [1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 1 1 1 1]
V =
1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 1 1 1 1
>> F = find(diff([0,V,0]));
>> [~,X] = find(diff(F)<4);
>> for k = X, V(F(k):F(k+1)-1) = 0; end
>> V
V =
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1