Signal rectification

I have an input signal, for example with the values:

0, 20, 40, 20, 0, -20, -40, -20, 0
As you can see its a sine wave.
What I would like is the first half of the wave only, but struggling to code it.
So if it starts positive, only have the positive part (rest zero).

In the same way, if the signal is: 0, -20, -40, -20, 0, 20, 40, 20, 0.
Then I only want the first negative half.

There can be any number of values within the wave (freq.).

Can anyone suggest some codecode to help?

As you can see its a sine wave.

Can I?

Have you already got these numbers in the machine? If so a simple

if(number > 0) { // store the number

will do it.
What do you want to do with these numbers?

Plot it in time domain and yeah it is a sine wave, power of imagination :wink:

Yeah I could store them, them, but the wave can have infinite values, so it almost needs to be in real time?

See the attached! :slight_smile:

See the attached!

I see a massively large picture that is difficult to view with some wiggly lines on it. I do not see any answers to my questions.

Plot it in time domain and yeah it is a sine wave, power of imagination

Looks like a triangle to me.

The point is we have no idea about what you want to do or what equipment you have.

ThomsonPA:
I have an input signal, for example with the values:

0, 20, 40, 20, 0, -20, -40, -20, 0
As you can see its a sine wave.

Irrespective of the waveform, I assume that the "negative" values are just those below a certain threshold but still positive with respect to ground and you are not actually feeding true negative signals to the ADC of your Arduino.

Yeah I could store them, them, but the wave can have infinite values, so it almost needs to be in real time?

The if-statement is your starting-point. What you do based on the results of the if-statement is up to you.

so it almost needs to be in real time?

Where are the real time negative numbers coming from? ..The ADC doesn't go negative.

So if it starts positive, only have the positive part (rest zero).

This will convert all of your negative numbers to zero:

if(number > 0)
     number = 0;

An if-statement is one way to throw-away the negative values. For full-wave rectification (converting negative values to positive) you can use the [u]absolute value[/u] function.

There can be any number of values within the wave (freq.).

Yes... But without knowing the sample rate, there is no frequency... There is only a sine wave (approximation) with no time scale. i.e. CDs are sampled at 44.1kHz, so a 22,050Hz signal has two samples per cycle (one per half-cycle). A 1kHz wave has about 44 samples per cycle. If you play it back at the wrong sample rate, the speed & frequency will be wrong (like playing-back a record at the wrong speed), WAV files have a sample-rate field in the header so they play back at the right speed/frequency. Here is an [u]Introduction to Digital Audio[/u]..