annotate aim-mat/tools/@signal/generate_kurtosis_noise.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 20ada0af3d7d
children
rev   line source
bleeck@3 1 % This external file is included as part of the 'aim-mat' distribution package
bleeck@3 2 % (c) 2011, University of Southampton
bleeck@3 3 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 4 % download of current version is on the soundsoftware site:
bleeck@3 5 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 6 % documentation and everything is on http://www.acousticscale.org
tomwalters@0 7
tomwalters@0 8 function [sig,x_pdf,y_pdf]=generate_kurtosis_noise(sig,kurtosis_wanted)
tomwalters@0 9 % generate a signal with a given kurtosis
tomwalters@0 10
tomwalters@0 11 % do we want to see anything?
tomwalters@0 12 grafix=0;
tomwalters@0 13
tomwalters@0 14 v=get(sig);
tomwalters@0 15 % variance_wanted=get(params,'Variance');
tomwalters@0 16 % skwednes=get(params,'Skewedness');
tomwalters@0 17
tomwalters@0 18
tomwalters@0 19
tomwalters@0 20
tomwalters@0 21 % change the kurtosis input to something meaningful
tomwalters@0 22 % these values are measured for different inputs of x
tomwalters@0 23 % if raw value for "Kurtosis" is inputted as:
tomwalters@0 24 input_kurt=[1 0.95 0.9 0.85 .8 .7 .6 .5 .4 .3 .25 .2 .15 .125 0.1 ];
tomwalters@0 25 % then the Kurtosis will be:
tomwalters@0 26 output_kurt=[-.05 0.04 0.13 0.26 .4 .7 1.12 1.72 2.58 4 5.11 6.87 9.6 11.8 15];
tomwalters@0 27
tomwalters@0 28 % to input the real Kurtosis, we need to turn these values around:
tomwalters@0 29 % and find the "input" value for a given Kurtosis:
tomwalters@0 30 kurtosis=interp1(output_kurt,input_kurt,kurtosis_wanted);
tomwalters@0 31
tomwalters@0 32 % % and the same for the variance. changing the variance does not change the
tomwalters@0 33 % % kurtosis
tomwalters@0 34 % input_var=0:15;
tomwalters@0 35 % output_var=[0.97 .68 .53 .43 .35 .3 .27 .24 .22 .2 .18 .17 .15 .14 .13 .12];
tomwalters@0 36 % variance=interp1(output_var,input_var,variance_wanted);
tomwalters@0 37
tomwalters@0 38 variance=1;
tomwalters@0 39
tomwalters@0 40
tomwalters@0 41
tomwalters@0 42 if grafix
tomwalters@0 43 figure(3)
tomwalters@0 44 clf
tomwalters@0 45 hold on
tomwalters@0 46 end
tomwalters@0 47
tomwalters@0 48 nr_points=2000;
tomwalters@0 49
tomwalters@0 50 stepx=10/nr_points;
tomwalters@0 51 % generate the pdf
tomwalters@0 52 x=-5:stepx:5;
tomwalters@0 53
tomwalters@0 54 p_pdf=pdf('Normal',x,0,1);
tomwalters@0 55 p_pdf=p_pdf/max(p_pdf);
tomwalters@0 56
tomwalters@0 57 % p_cdf=cumsum(p_pdf);
tomwalters@0 58 % p_cdf=p_cdf/max(p_cdf);
tomwalters@0 59 % if grafix
tomwalters@0 60 % plot(x,p_pdf,'b.');hold on
tomwalters@0 61 % end
tomwalters@0 62
tomwalters@0 63 % scale along x to change the variance:
tomwalters@0 64 y=p_pdf;
tomwalters@0 65
tomwalters@0 66
tomwalters@0 67 new_x=x*variance;
tomwalters@0 68 ny=interp1(new_x,y,x);
tomwalters@0 69 ny(isnan(ny))=0;
tomwalters@0 70
tomwalters@0 71 x_pdf=x; % save for plotting
tomwalters@0 72 y_pdf=ny;
tomwalters@0 73
tomwalters@0 74 % % and the change in y to modify the Kurtosis
tomwalters@0 75 %
tomwalters@0 76 % generate random numbers by reversing the cdf
tomwalters@0 77 new_cdf=cumsum(ny); %calculate the pdf:
tomwalters@0 78 new_cdf=new_cdf/max(new_cdf); % and normalise
tomwalters@0 79
tomwalters@0 80 for i=1:nr_points/2-1
tomwalters@0 81 % dx=abs(x(nr_points/2-i));
tomwalters@0 82 dy=abs(0.5-new_cdf(i));
tomwalters@0 83 ndy=power((2*dy),kurtosis);
tomwalters@0 84 new_cdf(i)=0.5-ndy/2;
tomwalters@0 85 new_cdf(nr_points-i)=0.5+ndy/2;
tomwalters@0 86 end
tomwalters@0 87
tomwalters@0 88
tomwalters@0 89 if grafix
tomwalters@0 90
tomwalters@0 91 old_cdf=cumsum(y);
tomwalters@0 92 old_cdf=old_cdf/max(old_cdf); % and normalise
tomwalters@0 93
tomwalters@0 94 plot(x,old_cdf,'-b.');
tomwalters@0 95 plot(x,new_cdf,'-r.'); hold on
tomwalters@0 96 legend({'cumulative normal distribution';'modfied cdf'},2);
tomwalters@0 97 end
tomwalters@0 98
tomwalters@0 99
tomwalters@0 100 serachy=1/nr_points:1/nr_points:1;
tomwalters@0 101 rev_cdf=zeros(1,nr_points-1);
tomwalters@0 102
tomwalters@0 103
tomwalters@0 104
tomwalters@0 105 for i=1:nr_points-1
tomwalters@0 106 rev_cdf(i)=find(new_cdf>serachy(i),1,'first');
tomwalters@0 107 end
tomwalters@0 108
tomwalters@0 109 % clf
tomwalters@0 110 % plot(rev_cdf,'r.');
tomwalters@0 111
tomwalters@0 112 r=round(rand(length(v),1)*(nr_points-1)+0.5);
tomwalters@0 113
tomwalters@0 114 rev_p=rev_cdf(r);
tomwalters@0 115
tomwalters@0 116 % clf
tomwalters@0 117 % hist(rev_p,100)
tomwalters@0 118
tomwalters@0 119
tomwalters@0 120 % and normalise to the right output range
tomwalters@0 121 rev_p=(rev_p-nr_points/2)/nr_points*10;
tomwalters@0 122
tomwalters@0 123 % return
tomwalters@0 124 % v=random('norm',0,sqrt(variance),length(v),1);
tomwalters@0 125
tomwalters@0 126
tomwalters@0 127 sig=set(sig,rev_p');
tomwalters@0 128