%% DON"T USE is ROB's MAGIC STUFF
%%% AKROYD'S AUDITORYTOOLBOX
%%restoredefaultpath; matlabrc 
clear all
clc
%%close all
warning off all

% addpath( ...
%     genpath(fullfile(   'H:\','\D_DRIVE\MATL_DIR\psignifit\')), ...
%                         'H:\D_DRIVE\PLOS_ONE_2007\MATLAB');
%                     
% cd 'H:\D_DRIVE\PLOS_ONE_2007\MATLAB\';


%%% MAC
addpath( ...
    genpath(fullfile(   'Y:','\MATLAB_STUFF\auditory_toolbox\')), ...
                        'D:','\LAB_2008\AKEROYD\');
                    
cd 'D:\LAB_2008\';




%%
%48828.125


wave = mcreatetone(1000, 1, 1, 0, 0, 500, 50, 48800, 1);
mwaveplay(wave, -1, 'stereo',1)
mwaveplot(wave, 'stereo', -1, -1);

mwavesave('1kHz.wav', wave, -1, 'stereo', 1)

%%%What does it do?

%   Other examples are: 
%   to make a 500-hz tone, of 60-dB power in each channel,
%   of 500-us ITD, o-degrres IPD, 250-ms duiration, 10-ms gates, 
%   and 20000-Hz sampling rate, type:
%   >> wave1 = mcreatetone(500, 60, 60, 500, 0, 250, 10, 20000, 1);
%  
%   to use an IID of 10 dB instead of a 500-ms ITD, type:
%   >> wave1 = mcreatetone(500, 50, 60, 0, 0, 250, 20, 20000, 1);
%  
%   to use an IPD of 90 degrees instead of an ITD or IID, type:
%   >> wave1 = mcreatetone(500, 60, 60, 0, 90, 250, 20, 20000, 1);
%  
%%  GENTONE   GENTONE GENTONE

% GenerateTtone Stimulus
%
% STM = GENTONE (<N>, <Freq>, <NEnvelope>, <Fs>, <grph>)
%
% Generate a sine-shaped tone, with
% N         - number of samples                 [7500]      samples
% Freq      - Frequency of tone                 [2000]      Hz
% NEnvelope - number of samples in envelope     [250]       samples
%             (head and tail are both 'NEnvelope')
% Fs        - Sample frequency                  [48828.125] Hz
% grph      - set to 1 to display stuff         [0]

%%%%%CREATE 1Khz TONE
%%%%%CREATE 1Khz TONE
%%%%%CREATE 1Khz TONE
%%%%%CREATE 1Khz TONE
%%%%%CREATE 1Khz TONE
%DURATION IN MSEC
dur=500; %%%[msec]
%%%NUMBER OF SAMPLES
%%%%%length(Sine3000)*((1/48828.125)*1000)
Fs=48828.125;
N=dur/((1/Fs)*1000);
Fsig=1000;  %%% SIGNAL FREQUENCY
Ramp_duration= 20; %% in [msec]
N_Ramp=ceil(Ramp_duration/((1/Fs)*1000));
Sine1000 = gentone(N,Fsig,N_Ramp,Fs,0.99);   


Nzero = zeros(1,978);		 %20 msec / ((1/Fs)*1000) %This will create a vector with 978 zeros
Sine1000 = [Nzero Sine1000]; %This “prepends” the zerovector to the Noise
wavplay(Sine3000,50000);

writewav(Sine1000,'snd001kHz.wav');

%%



%%%%%CREATE BB NOISE
% generates a broad-band noise between 20 (default) and 20 kHz with
% duration 150 msec (7500 samples / (25000 samples/sec*2) *1000). The
% on- and offset ramp each contain 250 samples = 250/50000 sec = 5 msec.
% This noise will be stored in the WAV-file 'BB.wav'.
%DURATION IN MSEC
dur=500; %%%[msec]
%%%NUMBER OF SAMPLES
%%%%%length(Sine3000)*((1/48828.125)*1000)
Fs=48828.125;
N=dur/((1/Fs)*1000);
Ramp_duration= 20; %% in [msec]
N_Ramp=ceil(Ramp_duration/((1/Fs)*1000));
Fcut_off=20000;  %%% Cut off Freq;
    stm = gengwn(N,N_Ramp,100,Fcut_off,Fs,1)

                figure(1)
                subplot(211)
                plot(stm)
                xlabel('Sample number')
                ylabel('Amplitude (a.u.)');

                Nfft = 2^10;
                Nnyq = Nfft/2;
                s = fft(stm,Nfft);
                s = abs(s);
                s = s(1:Nnyq);
                f = (0:(Nnyq-1))/(Nnyq)*Fs;
                subplot(212)
                loglog(f,s);
                set(gca,'Xtick',[1 2 4 6 12 24]*1000,'XtickLabel',[1 2 4 6 12 24]);
                xlabel('Frequency (Hz');
                ylabel('Amplitude (au)');	

Nzero = zeros(1,978);		 %20 msec / ((1/Fs)*1000) %This will create a vector with 978 zeros
stm = [Nzero stm];           %This “prepends” the zerovector to the Noise
wavplay(stm,Fs);

writewav(stm,'snd001BB.wav',0.999); 




%%
%HP = highpassnoise(stm);
LP = lowpassnoise(stm);			

Nzero = zeros(1,978);		%This will create a vector with 978 zeros
                            %This “prepends” the zerovector to the Noise
%HP = [Nzero HP];
LP = [Nzero LP];

 



%%wavplay(HP,Fs);
wavplay(LP,Fs);
%writewav(HP,'snd001HP.wav');
writewav(LP,'snd001LP.wav');



