Adding a Vector at Certain Points of a Sequence

3 views (last 30 days)
Troy Handlovic
Troy Handlovic on 6 Jul 2022
Edited: Jan on 6 Jul 2022

I am trying to simulate an expirnment, where a signal (gaussian peak) is added to itself in accordance to a random binary sequence. I have already generated the binary sequence, but need assistance with the addtion of the signal to itself.
Details:
Binary sequence is 0 or 1 at a 50% probablity.
The gaussian peak is generate by normpdf with a total x value of 1000.
When the binary sequence is at 1, I would like for the gaussian peak to be added to the original and when the binary sequence is at 0 nothing happens. This means that the sequence's start point would be offset by the location of the 1. So, if the 1 is the at location 100 of the sequence, the gaussian peak would have to start 100 points ahead of the orginal.
The signals need to be added to eachother sequentially. This means the third 1 value must be added to a signal that already has the first two 1 values summed into it.

  4 Comments
Troy Handlovic
Troy Handlovic on 6 Jul 2022
Thanks for the help.
I essentially want to add 0s to the original sequence to offset its stating point according to the random binary sequence and then sum all of the offset signals up. Here is a small example but I would like to do this on the scale of thousands of points with a more complicated signal. The final output should be 2x the original minus 1.
Sequence:
1010011001
Signal:
0001210000
Output
0001210000 first 1, original signal
000001210000 second 1
000000001210000 third 1
0000000001210000 fourth 1
0000000000001210000 fifth 1
Then sum them up for an output of
0001222113311210000

Sign in to comment.

Answers (1)

Jan
Jan on 6 Jul 2022
Edited: Jan on 6 Jul 2022
seq = [1 0 1 0 0 1 1 0 0 1];
signal = [0 0 0 1 2 1 0 0 0 0];
seq = randi([0,1], 1, 1e5);
signal = randi([0, 10], 1, 1e2)
signal = 1×100
10 4 4 9 10 2 0 4 8 8 3 6 5 8 2 6 1 7 0 6 6 6 10 7 7 10 3 2 6 2
w = numel(signal);
q = find(seq);
result = zeros(1, q(end) + w - 1);
for k = 1:numel(q)
p = q(k);
result(p:p+w-1) = result(p:p+w-1) + signal;
end
Walter has mentioned CONV:
result2 = conv(seq, signal); % Size is: numel(seq)+numel(signal)-1
result2 = result2(1:find(seq, 1, 'last') + w - 1); % Crop trailing non-signal
isequal(result, result2)
ans = logical
1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by