function x=tone(freq,level,dur,fs, Env)
%
% TONE generates a tone
%
% USAGE
%     x=tone(freq,level,dur,fs,Env)
%
% All parameters are optional, with defaults:
%
%   freq      frequency     (1 kHz)
%   level     level         (70 dB re 80dB=1)
%   dur       duration      (200 ms)
%   fs        sampling freq (50000 Hz)
%   Nenv      envelope length (5 ms)
%
if nargin < 5
   Env = 5;
   end

if nargin < 4
  fs=50000;
end  

if nargin < 3
  dur = 200;
end

if nargin < 2
  level = 70;
end

if nargin < 1
  freq = 1000;
end

sdur=round(fs*dur/1000);
Nenv = (fs*Env)/1000;

y=sin(2*pi*(freq/fs)*(1:sdur));
y=y';
y=envelope(y,Nenv);

x=db2amp(level,80).*[y, y]; % two channels

if dur>50 pdur=fs*50/1000;
else pdur=fs*dur/1000;
end

p50=fs*50/1000;
figure(1)
clf
plot(x(1:pdur,1));   % plot up to the first 50 ms of the tone
axis([0 p50 -1 1]);
set(gca, 'XTick',[0 p50/5 2*p50/5 3*p50/5 4*p50/5 p50]);
set(gca, 'XTickLabel',[' 0';'10';'20';'30';'40';'50']);
xlabel('Time [ms]', 'FontSize', 12);
ylabel('Amplitude [au]','FontSize',12);


return
