MATLAB: How to add 0 values before and after a sine wave

sine wave zero values

Hi,
I have created a sine wave as follow:
Fs=2000;
t=0:1/Fs:1-1/Fs
f0=100;
y=sin(2*pi*f0*t)
Now, I would like to add 10 points with their value = 0 just before and after this sine wave. How could I do that ?
Thanks in advance!

Best Answer

It's not exactly clear what you're asking for but here are two solutions.
If you just want to add 10 points to the beginning and end of your entire signal
% number of elements to insert

nAdded = 10;
% value to use as filler

filler = 0;
% insert the values
fillVec = repmat(filler, 1, nAdded);
expandedY = [fillVec, y, fillVec]; %Here's your new vector
If you want to add 10 points between each cycle of the sine curve including the beginning and end
% number of elements to insert
nAdded = 10;
% value to use as filler
filler = 0;
% lenght of 1 period (number of indices)
pdLength = length(y)/f0;
% insertion indices
origIdx = pdLength : pdLength : length(y); %indices of y
expandedIdx = origIdx + cumsum(repmat(nAdded,1,length(origIdx))); %indices of expanded y
origIdx = [0, origIdx];
expandedIdx = [0, expandedIdx];
% create new vector
expandedY = repmat(filler, 1, length(y) + f0*nAdded+nAdded); %expanded y value
for i = 1 : length(expandedIdx)-1
expandedY(expandedIdx(i)+nAdded+1 : expandedIdx(i+1)) = y(origIdx(i)+1 : origIdx(i+1));
end