# Vector operation in GRC

I have a vector quantity (size 1024), and I want to do a calculation
on it such that v_out[i] = v_in[i] - v_in[i-1]

Is such a calculation possible in GRC? I haven’t found any way to
get a hold of the individual elements of a vector.

@(^.^)@ Ed

I would probably try converting the vector to a stream, delay the stream
by 1 sample, subtract one stream from the other, and then convert the
resulting (difference) stream back to a vector.

-Cam

Will that preserve the absolute positions of the original vector
elements? The initial vector represents an FFT, so it’s
important to me that I keep the same positions.

@(^.^)@ Ed

Hmm… I’m not sure about that. Seems reasonable to assume that the
stream-to-vector and vector-to-stream blocks would be inverses of each
other. Mostly I’d just be worried about the first or last subtraction -
you might only get a length 1023 vector out at the end.

When you link it up, can you tell whether it produces reasonable
results?

If you’re somewhat familiar with how to write/modify blocks, it probably
wouldn’t be difficult to implement that functionality by making a new
block that is a copy of gr_multiply_const_vcc.cc (for instance) and
changing the work function to do what you want.

Sean

I did a hand-contrived case with a simple 16-element vector, and it
seems to work.

@(^.^)@ Ed

Yes, I can do my own blocks. It would be pretty trivial to do it
there. I just wanted to know if there was an easy way to do it
solely using GRC.

@(^.^)@ Ed

And of course, you can always put a GRC wrapper around your custom
block…

If you’re somewhat familiar with how to write/modify blocks, it probably
wouldn’t be difficult to implement that functionality by making a new block that
is a copy of gr_multiply_const_vcc.cc (for instance) and changing the work
function to do what you want.

Sean

Here’s a vector-subtract graph that should do what Ed wants, might need
some tweaking.

Performance won’t be as good as with a dedicated block to do the same
thing, but if the data rates aren’t obscene, this should
work reasonably well, I think. Ed mentioned that the vector is the
result of an FFT, so I simply placed an FFT block in there for
illustrative purposes.

And of course, you can always put a GRC wrapper around your custom block…

Indeed, and donate it back to the community…

Marcus L.
Principal Investigator