sr = 44100;
gatedur = .01; % the duration of the gate in seconds (i.e., 10 ms)
gate = cos(linspace(pi, 2*pi, sr*gatedur));

f = 250; % frequency of the tone in Hz
d = .5; % duration of the tone in seconds
time = linspace(0, d, sr*d); % a time vector
tone = sin(2*pi*f*time);

sound(tone, sr)
pause(1)

gate = gate+1; % this operation translates all the values of the modulator to the 0/+2 range
gate = gate/2; % this operation compresses the values within the 0/+1 range

offsetgate = fliplr(gate);

sustain = ones(1, (length(tone)-2*length(gate)));
envelope = [gate, sustain, offsetgate];
smoothed_tone = envelope .* tone;

sound(smoothed_tone, sr)
subplot(3, 1, 1); plot(time, tone)
subplot(3, 1, 2); plot(time, envelope, 'o')
subplot(3, 1, 3); plot(time, smoothed_tone)