# How can I remove negative values from vector (Half-Wave Rectification)

13 views (last 30 days)
I need help removing the negative values from a vector. I I started with an EMG signal and converted the signal from the time domain to the frequency domain using fft. Next I applied a bandpass filter from 25-400 Hz to get a filtered signal. Now I need to apply a half wave rectification. I would like to use a for loop to remove the ngative values from v_mag_filtered. I'd appreciate the help, I'm a bit rusty on MATLAB
% Time Signal
max_index = 10000;
freq = 960;
t_step = 1/freq;
for i = 1:max_index
t(i) = (i-1)*t_step;
v(i) = EMG(i) * 1E-6;
end
% Power spectrum
for i = 1:max_index
freq_range(i) = (1/t_step)*(i-1)/max_index;
end
v_freq = fft(v,max_index); %complex vector of ECG singal
v_mag = 2*v_freq.*conj(v_freq)/(max_index)^2; %phase information of complex number removed
x = freq_range(1:max_index/2);
y = v_mag(1:max_index/2);
%band pass filter
v_freq_filtered = v_freq;
for i = 1:max_index
if ((freq_range(i)<25))
v_freq_filtered(i) = 0.0;
end
if ((freq_range(i)>400))
v_freq_filtered(i) = 0.0;
end
end
v_mag_filtered = 2*v_freq_filtered.*conj(v_freq_filtered)/(max_index)^2;

madhan ravi on 3 Nov 2020
vector = [1, -1];
vector( vector < 0 ) = 0 % [] to remove

Star Strider on 3 Nov 2020
Try this:
t = linspace(0, 6*pi, 1E+5);
s = sin(t);
idx = s >= 0; % Conditional Logical Index
s_hwr = s.*idx; % Signal With Half-Wave Rectification
figure
plot(t, s_hwr)
grid
ylim([-1 1])
This will work regardless of your signal. Since ‘EMG’ is not provided, I use ‘s’ instead here.
##### 2 CommentsShowHide 1 older comment
Star Strider on 4 Nov 2020
Sure!
I chose 3 cycles to provide a representative waveform, and elements to produce a smooth plot. All of these (in this example) are arbitrary, so choose whatever values you want (within reason, and within the available memory capacity of your computer) to experiment with.

m.venkata manohar reddy on 1 Aug 2021
Find the Fourier series of the function obtained by
passing the sinusoidal voltage v(t) = v0 cos(100πt)
through a half wave rectifier given in figure that clips
the negative waves