// Spektralanalyse FFT, Power zweidimensional 10Sekunden
// Name: Analyse-10Sekunden-V-220824.sce
// Datum: 22.08.2024
// Pegelabhängige Filterung bei Leistungsspektrum
// Bandpass Filterung
// Shift Play für tiefe Frequenzen
//
///////////////////////////////////////////////////////////////////////////////
////////                      VARIABLEN                            ////////////
///////////////////////////////////////////////////////////////////////////////
mclose('all');
clear;
close();
z=struct('datei','','pfad','','pfaddatei','','info',[0 0 0],'samplerate',0,'laufzeit',0,'kanal',0,'position',0,'start1',0,'start2',0,'graf1',0,'graf2',0,'graf3',0,'graf4',0,'graf5',0,'graf6',0,'grafschw',0,'W1',0,'W2',0,'W3',0,'F0',0,'F1',0,'F2',0,'F3',0,'P1',0,'S1',0,'S2',0,'filter1',1,'filter2',2000,'gramax',0,'gramin',0,'bp',0,'posi',0,'verst',1,'low',1,'high',20000,'fen1',0,'fen2',0,'level_0',0,'tip',0);
//
z.F0=zeros(1,480000);
z.F1=z.F0 //das original FFT
z.F2=z.F0 // das angezeigte FFT
z.F3=z.F0 // das gefilterte FFT
z.W1=z.F0 // das original WAV
z.W2=z.F0 // das Ausgang WAV
z.W3=z.F0 // das transponierte WAV

///////////////////////////////////////////////////////////////////////////////
////////////                 SUBROUTINEN                           ////////////
///////////////////////////////////////////////////////////////////////////////
// NEU
function [z]=neu(z)
//////////////////////////////////////////////////////////////////////////////
// WAV Datei auswählen   D:\Mdaten\Testsignale/100Hz-03A_g.wav
if z.pfad=='' then
[z.datei,z.pfad]=uigetfile('*.wav','D:\Mdaten');    //WAV Datei wählen
else
[z.datei,z.pfad]=uigetfile('*.wav',z.pfad);    //WAV Datei wählen    
end
if isequal(z.datei,'') then return end   //Ist eine Date gewählt?
z.pfaddatei=z.pfad+'\'+z.datei;  //Vollständigen Pfad erstellen 
z.info = wavread(z.pfaddatei,'info');  //WAV Info`s laden
z.laufzeit=floor(z.info(8)/48000); // lauzeit in ganzen sekunden
z.samplerate=z.info(3); //Samplerate der Audiodatei
if z.info(3)<>48000 then z.datei='';z.pfad='';z.pfaddatei='';disp('//////////  Die Abtastfrequenz ist nicht 48000  //////////');return end
disp('//////////////////////////////////////////////////////////')
disp('WAV-Datei: '+z.pfaddatei); //Ausgabe vollständiger Pfad
disp('Kanäle: '+string(z.info(2)));   //Ausgabe Anzahl der Kanäle
disp('Samplerate: '+string(z.info(3))+' Abtastungen/Sekunde');   //Ausgabe Samplerate
disp('Dateilänge : '+string(z.info(8))+' Byte');    //Ausgabe Anzahl der Samples
disp('Laufzeit : '+string(z.info(8)/z.info(3))+' Sekunden');    //Laufzeit in Sek.
disp('//////////////////////////////////////////////////////////')
// WERTE INS FORMULAR SCHREIBEN
z.tip=' '+string(z.info(2))+' Kanäle / '+ string(z.info(8))+' Samples / '+string(z.info(3))+' Samplerate / '+string(z.info(8)/z.info(3))+' Laufzeit in Sekunden ';
set(se_0_0,'TooltipString',z.tip) 
// PFAD + DATEINAME
set(se_0_0,'string',string('  ')+z.pfaddatei);
//graphen sichtbar
set(se_1_1,'value',1);
set(se_1_3,'value',1);
// KANAL SETZEN
z.kanal=1;
set(se_2_1,'value',1);
set(se_2_2,'value',0);
// POSITION
z.posi=0;
set(se_4_2,'string',string(z.posi)+' / '+string(z.laufzeit));
z.start1=z.posi+1;
z.start2=z.start1+479999;
// VERSSTÄRKUNG
z.verst=1;
set(se_3_2,'value',z.verst); // slider
set(se_3_3,'string',string(z.verst)); // anzeige
// F-MAX
z.high=20000;
z.filter2=20000;
// F-MIN
z.low=1;
z.filter1=1;
//
[z]=rw1(z)
endfunction
/////////////////////////////////////////////////////////////////////////////
// rechenwerke
function [z]=rw1(z) //die Wav daten einlesen
////////////////////////////////////////////////////////////////////////////
// WAV DATEN EINLESEN
if z.start2>z.info(8) then 
    z.W1=wavread(z.pfaddatei,[z.start1 z.info(8)]); //wav daten einlesen
    i=size(z.W1);
    z.W1(1,i(2)+1:480000,1)=z.F0(1,480000-i(2)); // mit nullen auffüllen
else
    z.W1=wavread(z.pfaddatei,[z.start1 z.start2]); //wav daten einlesen
end
set(se_4_2,'string',string(z.posi)+' / '+string(z.laufzeit));
// KANAL WAHL
z.W1=z.W1(z.kanal,:);// den kanal einstellen
// VERSTÄRKUNG
z.W1=z.W1*z.verst;// die verstärkung des eingangssignals
//
if z.graf2<>0 then delete(z.graf2);z.graf2=0; end;
if z.graf3<>0 then delete(z.graf3);z.graf3=0; end;
if z.graf4<>0 then delete(z.graf4);z.graf4=0; end;
[z]=malen1(z)
// 
endfunction
/////////////////////////////
function [z]=rw2(z)
//FFT berechnen
//fft
z.F1=fft(z.W1,-1,'nonsymmetric');
z.F1(1,1)=0;//Gleichspannungsanteil auf 0 setzen
z.F1=z.F1/480000;
z.F2=abs(z.F1(1:20001));
z.F2=z.F2*2000;
// POWERSPEKTRUM
z.P1=z.F1.* conj(z.F1);
z.P1=abs(z.P1);
z.P1=z.P1(1:20001);
z.P1=z.P1*4000;
// 
[z]=malen3(z)
[z]=malen4(z)
endfunction
///////////////////////////////
function [z]=rw3(z)
//Bandpass berechnen
z.F3=z.F0;
z.filter1=z.low;
z.filter2=z.high;
z.F3(z.filter1:z.filter2)=z.F1(z.filter1:z.filter2);///////////////////////////////////////////////////////////////
z.W2=fft(z.F3,1,'nonsymmetric');/////////////////////////////////////////////////////////////////////////////////
z.W2=real(z.W2);
z.W2=z.W2*480000*2;
//prozent
z.S1=sum(abs(z.W1(2:480000)));
z.S2=sum(abs(z.W2(2:480000)));
z.p2=floor(z.S2*100/z.S1);
set(se_pro,'string',string(z.p2)+' % ' +'von Eing.');
[z]=malen2(z)  
[z]=malen5_6(z)  
[z]=rw4(z)
endfunction
////////////////////
function [z]=rw4(z)
//transponieren mit 100 Hz
y=z.F0;
y(1001:21000)=z.F3(2:(20001));
z.W3=fft(y,1,'nonsymmetric');
z.W3=real(z.W3);
z.W3=z.W3*480000*2;
endfunction
///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//malen1
function [z]=malen1(z)
///////////////////////////////////////////////////////////////////////////
//malen graph 1 in fenster 1
sca(z.fen1);
if z.graf1==0 then else delete(z.graf1) end;
plot2d(z.W1);
z.graf1=gce()
z.graf1.children.thickness=1;
z.graf1.children.foreground=color('blue');
[z]=sicht1(z); // sichtbarkeit
z.fen1.x_ticks=tlist(['locations','labels'],[0:48000:480000],string([z.posi:1:z.posi+10]));
//
endfunction

//malen graph 2 in fenster 1
function [z]=malen2(z)
///////////////////////////////////////////////////////////////////////////
// malen grap 2 in fenster 1
sca(z.fen1);
if z.graf2==0 then else delete(z.graf2) end;
plot2d(z.W2);
z.graf2=gce();
z.graf2.children.thickness=1;
z.graf2.children.foreground=color('red');
[z]=sicht2(z);// sichtbarkeit
//
endfunction
///////////////////////////////////////////////////////////////////////////
function [z]=malen3(z)
///////////////////////////////////////////////////////////////////////////
// malen grap 3
sca(z.fen2);
if z.graf3==0 then else delete(z.graf3) end;
plot2d2(0:20000,z.F2);
z.graf3=gce();
z.graf3.children.thickness=3;
z.graf3.children.foreground=color('scilabcyan4');
[z]=sicht3(z); // sichtbarkeit
//
endfunction
///////////////////////////////////////////////////////////////////////////
function [z]=malen4(z)
//////////////////////////////////////////////////////////////////////////
// malen graph 4 in fenster 2
sca(z.fen2);
if z.graf4==0 then else delete(z.graf4) end; 
plot2d2(0:20000,z.P1);
z.graf4=gce();
z.graf4.children.thickness=3;
z.graf4.children.foreground=color('scilabmagenta2');
[z]=sicht4(z); // sichtbarkeit
//
endfunction
///////////////
function [z]=malen5_6(z)
//////////////////////////////////////////////////////////////////////////
// malen graph 5 und 6 in fenster 2
sca(z.fen2);
//low
if z.graf5==0 then else delete(z.graf5) end; 
plot2d([z.filter1 z.filter1],[0 1000]);
z.graf5=gce();
z.graf5.children.thickness=3;
z.graf5.children.foreground=color('darkolivegreen');
set(se_6_7,'string',string(z.low/10));
// high
if z.graf6==0 then else delete(z.graf6) end; 
plot2d([z.filter2 z.filter2],[0 1000]);
z.graf6=gce();
z.graf6.children.thickness=3;
z.graf6.children.foreground=color('sienna');
set(se_6_3,'string',string(z.high/10));
//
endfunction

//
function [z]=malenmax(z)
//filtergrenze max setzen 
sca(z.fen2);
if z.gramax==0 then else delete(z.gramax);z.gramax=0; end;
plot2d([z.high z.high],[0 1000]);
z.gramax=gce();
z.gramax.children.thickness=2;
z.gramax.children.foreground=color('orange');
//
endfunction
//
function [z]=malenmin(z)
//filtergrenze min setzen
sca(z.fen2);
if z.gramin==0 then else delete(z.gramin);z.gramin=0; end;
plot2d([z.low z.low],[0 1000]);
z.gramin=gce();
z.gramin.children.thickness=2;
z.gramin.children.foreground=color('greenyellow');
//
endfunction
///////////////////////////////////////////////////////////////////////////
function [z]=sicht1(z)
///////////////////////////////////////////////////////////////////////////
// graph1 ein/aus
if se_1_1.value==1 then z.graf1.visible='on' else z.graf1.visible='off' end
endfunction
//////////////////////////////////////////////////////////////////////////////
function [z]=sicht2(z)
///////////////////////////////////////////////////////////////////////////
// graph2 ein/aus
if se_1_3.value==1 then z.graf2.visible='on' else z.graf2.visible='off' end
endfunction
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
function [z]=sicht3(z)
///////////////////////////////////////////////////////////////////////////
// graph3 ein/aus
if se_fft.value==1 then z.graf3.visible='on' else z.graf3.visible='off' end
endfunction
//////////////////////////////////////////////////////////////////////////////
function [z]=sicht4(z)
//////////////////////////////////////////////////////////////////////////////
// graph 4 ein/aus
if se_pow.value==1 then z.graf4.visible='on' else z.graf4.visible='off' end
endfunction
////////////////////////////////////////////////////////////////////////

function [z]=kawal(z)
// kanalwahl 
i=se_2_1.value;
if i==1 then 
    set(se_2_2,'value',0);
    z.kanal=1;
    else
    set(se_2_2,'value',1);
    z.kanal=2;
end
if z.info(2)==0 then return end
if z.info(2)==1 then
    set(se_2_2,'value',0);
    set(se_2_1,'value',1);
    z.kanal=1;
end
[z]=rw1(z)
endfunction
///////////////////////////////////////
function [z]=kawa2(z)
// kanalwahl    
i=se_2_2.value;
if i==1 then 
    set(se_2_1,'value',0);
    z.kanal=2;
    else
    set(se_2_1,'value',1);
    z.kanal=1;
end 
if z.info(2)==0 then return end
if z.info(2)==1 then
    set(se_2_2,'value',0);
    set(se_2_1,'value',1);
    z.kanal=1;
end
[z]=rw1(z)
endfunction
///////////////////////////////////////////////////////////////////////////
//fenster 2 amplitudensteuerung
function [z]=fensta1000(z)
///////////////////////////////////////fenster 2 , amplitude 100
z.fen2.y_ticks=tlist(['locations','labels'],(0:100:1000),string(0:0.1:1));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=1000;
z.fen2.data_bounds =zh;
set(se_amp_1,'max',1000)
endfunction

//
function [z]=fensta500(z)
/////////////////////////////////////////// fenster 2 , amplitude 50
z.fen2.y_ticks=tlist(['locations','labels'],(0:50:500),string(0:0.05:0.50));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=500;
z.fen2.data_bounds =zh;
set(se_amp_1,'max',500)
endfunction
//
function [z]=fensta250(z)
/////////////////////////////////////////// fenster 2 , amplitude 25
z.fen2.y_ticks=tlist(['locations','labels'],[0:25:250],string(0:0.025:0.250));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=250;
z.fen2.data_bounds =zh;
set(se_amp_1,'max',250)
endfunction
//
function [z]=fensta100(z)
/////////////////////////////////////////fenster 2 , amplitude 10
z.fen2.y_ticks=tlist(['locations','labels'],(0:10:100),string(0:0.010:0.100));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=100;
z.fen2.data_bounds =zh;
set(se_amp_1,'max',100)
endfunction

//
function [z]=fensta50(z)
/////////////////////////////////////////fenster 2 , amplitude 10
z.fen2.y_ticks=tlist(['locations','labels'],(0:5:50),string(0:0.005:0.05));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=50;
z.fen2.data_bounds =zh;
set(se_amp_1,'max',50)
endfunction
//
function [z]=fensta25(z)
/////////////////////////////////////////fenster 2 , amplitude 5
z.fen2.y_ticks=tlist(['locations','labels'],(0:2.5:25),string(0:0.0025:0.025));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=25;
z.fen2.data_bounds =zh;
set(se_amp_1,'max',25)
endfunction
//
function [z]=fensta10(z)
//////////////////////////////////////////// fenster 2 , amplitude 1
z.fen2.y_ticks=tlist(['locations','labels'],(0:1:10),string(0:0.001:0.01));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=10;
z.fen2.data_bounds =zh;  
set(se_amp_1,'max',10)
endfunction
//
function [z]=fensta5(z)
//////////////////////////////////////////// fenster 2 , amplitude 1
z.fen2.y_ticks=tlist(['locations','labels'],(0:0.5:5),string(0:0.0005:0.005));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=5;
z.fen2.data_bounds =zh; 
set(se_amp_1,'max',5) 
endfunction
//
function [z]=fensta2(z)
//////////////////////////////////////////// fenster 2 , amplitude 1
z.fen2.y_ticks=tlist(['locations','labels'],(0:0.25:2.5),string(0:0.00025:0.0025));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=2.5;
z.fen2.data_bounds =zh;  
set(se_amp_1,'max',2.5)
endfunction
//
function [z]=fensta01(z)
//////////////////////////////////////////// fenster 2 , amplitude 1
z.fen2.y_ticks=tlist(['locations','labels'],(0:0.1:1),string(0:0.0001:0.001));
zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=1;
z.fen2.data_bounds =zh;  
set(se_amp_1,'max',1)
endfunction
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//fenster 2 frequenzsteuerung
//fenster 2 frequenzsteuerung
function [z]=fenstf2000(z)
/////////////////////////////////// fenster 2 , frequenzachse 2000
z.fen2.x_ticks=tlist(['locations','labels'],(0:500:20000),string(0:50:2000));
z.fen2.sub_tics=[9,9,9];
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=20000;
z.fen2.data_bounds =zh;
set(se_6_1,'max',20000);
set(se_6_5,'max',20000);
endfunction

//
function [z]=fenstf1000(z)
///////////////////////////////////// fenster 2 , frequenzachse 1000
z.fen2.x_ticks=tlist(['locations','labels'],(0:250:10000),string(0:25:1000));  
z.fen2.sub_tics=[9,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=10000;
z.fen2.data_bounds =zh;
set(se_6_1,'max',10000);
set(se_6_5,'max',10000);
endfunction
//
function [z]=fenstf500(z)
///////////////////////////////////// fenster 2 , frequenzachse 500
z.fen2.x_ticks=tlist(['locations','labels'],(0:250:5000),string(0:25:500));  
z.fen2.sub_tics=[9,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=5000;
z.fen2.data_bounds =zh;
set(se_6_1,'max',5000);
set(se_6_5,'max',5000);
endfunction
//
function [z]=fenstf250(z)
///////////////////////////////////// fenster 2 , frequenzachse 250
z.fen2.x_ticks=tlist(['locations','labels'],(0:100:2500),string(0:10:250));  
z.fen2.sub_tics=[9,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=2500;
z.fen2.data_bounds =zh;
set(se_6_1,'max',2500);
set(se_6_5,'max',2500);
endfunction
//
function [z]=fenstf120(z)
///////////////////////////////////// fenster 2 , frequenzachse 120
z.fen2.x_ticks=tlist(['locations','labels'],(0:50:1200),string(0:5:120));  
z.fen2.sub_tics=[9,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=1200;
z.fen2.data_bounds =zh;
set(se_6_1,'max',1200);
set(se_6_5,'max',1200);
endfunction
//
function [z]=fenstf60(z)
///////////////////////////////////// fenster 2 , frequenzachse 60
z.fen2.x_ticks=tlist(['locations','labels'],(0:10:600),string(0:1:60));  
z.fen2.sub_tics=[9,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=600;
z.fen2.data_bounds =zh;
set(se_6_1,'max',600);
set(se_6_5,'max',600);
endfunction
//
function [z]=fenstf30(z)
////////////////////////////////////// fenster 2 , frequenzachse 30
z.fen2.x_ticks=tlist(['locations','labels'],(0:10:300),string(0:1:30));  
z.fen2.sub_tics=[9,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=300;
z.fen2.data_bounds =zh;
set(se_6_1,'max',300);
set(se_6_5,'max',300);
endfunction
//
function [z]=fenstf12(z)
////////////////////////////////////// fenster 2 , frequenzachse 12
z.fen2.x_ticks=tlist(['locations','labels'],(0:5:120),string(0:0.5:12));  
z.fen2.sub_tics=[4,9,9];      
zh=z.fen2.data_bounds;
zh(1,1)=0;
zh(2,1)=120;
z.fen2.data_bounds =zh;
set(se_6_1,'max',120);
set(se_6_5,'max',120);
endfunction////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
function [z]=p_r_10(z)
//position steuerung test
if isequal(z.datei,'') then return end   //Ist eine Date gewählt?
if z.start2+480000>z.info(8) then
else
z.start1=z.start1+480000;
z.start2=z.start2+480000;  
z.posi=z.posi+10;
[z]=rw1(z)
end

endfunction
////////
function [z]=p_r_1(z)
//position steuerung test
if isequal(z.datei,'') then return end   //Ist eine Date gewählt?
if z.start2+48000>z.info(8) then
else
z.start1=z.start1+48000;
z.start2=z.start2+48000;  
z.posi=z.posi+1;
[z]=rw1(z)
end
endfunction
//////////////////////////////////////////
function [z]=p_l_10(z)
//position steuerung test
if isequal(z.datei,'') then return end   //Ist eine Date gewählt?
if z.start1<480000 then
else
z.start1=z.start1-480000;
z.start2=z.start2-480000;
z.posi=z.posi-10;
[z]=rw1(z)
end
endfunction
//////////////////////////
function [z]=p_l_1(z)
//position steuerung test
if isequal(z.datei,'') then return end   //Ist eine Date gewählt?
if z.start1<48000 then
else
z.start1=z.start1-48000;
z.start2=z.start2-48000;
z.posi=z.posi-1;
[z]=rw1(z)
end
endfunction
//////////////////////////////////////////////////////////////////////////////////////////////////////
function [z]=bp(z)
//bandpass 
if z.high < z.low then
i=z.low
z.low = z.high
z.high = i
end

if z.high == z.low then
if z.low == 1 then z.high=z.low+1 end
if z.high == 20000 then z.low=z.high-1 end
end
set(se_6_2,'string',string(z.high/10))
set(se_6_6,'string',string(z.low/10))
[z]=malenmax(z)
[z]=malenmin(z)
z.filter1=z.low
z.filter2=z.high
set(se_6_1,'string',string(z.high))
set(se_6_5,'string',string(z.low))
endfunction
//////////////////////////////////////////////////////
function [z]=verstark(z)
//verstärkung
z.verst=se_3_2.value;
set(se_3_3,'string',string(z.verst));
[z]=rw1(z)
endfunction
//
///////////////////////////////////////////////////////////////////////////
function [z]=level_1(z)
//schwelle setzen
z.level_0=se_amp_1.value
sca(z.fen2);
if z.grafschw==0 then else delete(z.grafschw) end;
plot2d([0 20000],[z.level_0 z.level_0]);
z.grafschw=gce();
z.grafschw.children.thickness=2;
z.grafschw.children.foreground=color('mediumaquamarine');
endfunction
//////////////////////////////////////////////////////////////////////////
function [z]=level_2(z)
sca(z.fen2);
if z.graf5==0 then else delete(z.graf5);z.graf5=0; end;
if z.graf6==0 then else delete(z.graf6);z.graf6=0; end;
if z.grafschw==0 then else delete(z.grafschw) end;
plot2d([0 20000],[z.level_0 z.level_0]);
z.grafschw=gce();
z.grafschw.children.thickness=2;
z.grafschw.children.foreground=color('seagreen');
// schwelle berechnen und anwenden
z.F3=z.F0
z.F3(1:20001)=z.F1(1:20001);
z.F3(z.P1<z.level_0)=0
z.F3(1,1)=0
z.W2=fft(z.F3,1,'nonsymmetric');
z.W2=real(z.W2);
z.W2=z.W2*480000*2;
[z]=malen2(z)
///////////////////transformieren
y=z.F0
y(1001:21000)=z.F3(2:(20001));
z.F3=y;
z.W3=fft(z.F3,1,'nonsymmetric');
z.W3=real(z.W3);
z.W3=z.W3*480000*2;
//prozent
z.S1=sum(abs(z.W1(1:480000)));
z.S2=sum(abs(z.W2(1:480000)));
z.p2=floor(z.S2*100/z.S1);
set(se_pro,'string',string(z.p2)+' % ' +'von Eing.');
endfunction
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/////////                         S T A R T                      /////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FORMULAR ERSTELLEN
z.fo=figure('position',[10,10,1400,900]);
z.fo.background= 33;
z.fo.color_map = jet(1000); //Colormap festlegen
z.fo.visible = "on";
z.fo.figure_name="Analyse-2d-10Sekunden-V-220824.sce";
z.fo.BackgroundColor=[1 1 1];
z.fo.resize = "off"
///////////////////////////////////////////////////////////////////////////////
// CONTROLSS INS FORMULAR EINBAUEN
//////////////////////////////////////////////////////////////////////FENSTER 1
// EINGANG Ausgang ein aus
se_1_1=uicontrol(z.fo,"style","checkbox",'HorizontalAlignment','left','Relief','flat','TooltipString','Eingang Grafik Ein / Aus','position', [1300 860 90 15],'string','Graph Eing.','callback','[z]=sicht1(z)'); //Wav eingange ein / aus
se_1_2=uicontrol(z.fo,"style","text",'backgroundcolor',[0 0 1],'position', [1390 860 5 15]);// Farbmarkierung blau
se_1_3=uicontrol(z.fo,"style","checkbox",'HorizontalAlignment','left','Relief','flat','TooltipString','Ausgang Grafik Ein / Aus','position', [1300 840 90 15],'string','Graph Ausg.','callback','[z]=sicht2(z)'); //Wav eingange ein / aus
se_1_4=uicontrol(z.fo,"style","text",'backgroundcolor',[1 0 0],'position', [1390 840 5 15]);// Farbmarkierung rot
ts1=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 830 95 5]);// trennstrich grün
//
//kanal
se_2_1=uicontrol(z.fo,"style","checkbox",'HorizontalAlignment','left','Relief','flat','TooltipString','Umschaltung zwischen Kanal 1 und Kanal 2','position', [1300 810 90 15],'string','Kanal 1','callback','[z]=kawal(z)'); //kanal 1 ein / aus
se_2_2=uicontrol(z.fo,"style","checkbox",'HorizontalAlignment','left','Relief','flat','TooltipString','Umschaltung zwischen Kanal 1 und Kanal 2','position', [1300 790 90 15],'string','Kanal 2','callback','[z]=kawa2(z)'); //kanal 2 ein / aus
se_2_3=uicontrol(z.fo,"style","text",'backgroundcolor',[0.419 0.628 0.333],'position', [1390 790 5 35]);// Farbmarkierung dunkelgrün
ts2=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 780 95 5]);// trennstrich grün
//
//verstärkung
se_3_1=uicontrol(z.fo,"style","text",'HorizontalAlignment','center' ,'position', [1300 760 90 15],'string','Verstärkung'); //Titel Verstärkung
se_3_2=uicontrol(z.fo,"style","slider",'HorizontalAlignment','left','Relief','flat','TooltipString','Eingang Verstärkung 1 - 10','min',1,'max',10,'SnapToTicks','on','sliderstep',1,'value',1,'position', [1320 740 70 15],'callback','[z]=verstark(z)'); //slider verstärkung
se_3_3=uicontrol(z.fo,"style","text",'HorizontalAlignment','center' ,'position', [1300 740 20 15],'string',''); //zahl verstärkung
se_3_4=uicontrol(z.fo,"style","text",'backgroundcolor',[0.419 0.628 0.333],'position', [1390 740 5 35]);// Farbmarkierung dunkelgrün
ts3=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 730 95 5]);// trennstrich grün
//
// POSITION
se_4_1=uicontrol(z.fo,"style","text",'HorizontalAlignment','center' ,'position', [1300 710 90 15],'string','Position');//position
se_4_2=uicontrol(z.fo,"style","text",'HorizontalAlignment','center' ,'TooltipString','Position / maximale Position','position', [1300 690 90 15],'string','0 / 0');//position
se_4_3=uicontrol(z.fo,"style","pushbutton",'Relief','flat','TooltipString','Position einstellen 10 Sek. rechts', 'position', [1368 670 24 15],'string','>>','callback','[z]=p_r_10(z)'); //>>
se_4_4=uicontrol(z.fo,"style","pushbutton",'Relief','flat', 'TooltipString','Position einstellen 1 Sek. rechts','position', [1345 670 24 15],'string','>','callback','[z]=p_r_1(z)');//>
se_4_5=uicontrol(z.fo,"style","pushbutton",'Relief','flat','TooltipString','Position einstellen 10 Sek. links', 'position', [1300 670 24 15],'string','<<','callback','[z]=p_l_10(z)');//<<
se_4_6=uicontrol(z.fo,"style","pushbutton",'Relief','flat','TooltipString','Position einstellen 1 Sek. links', 'position', [1323 670 24 15],'string','<','callback','[z]=p_l_1(z)');//<
se_4_7=uicontrol(z.fo,"style","text",'backgroundcolor',[0.419 0.628 0.333],'position', [1390 670 5 55]);// Farbmarkierung dunkelgrün
ts5=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 660 95 5]);// trennstrich grün
//
// ////////////////////////////////
// NEU
se_5_1=uicontrol(z.fo,"style","pushbutton",'Relief','raised','TooltipString','Neue WAV Datei auswählen. Dateien die weniger als 10 Sekunden Laufzeit haben werden um 10 Sekunden verlängert und als Kopie abgespeichert und die Kopie geladen.','position', [1300 620 75 20],'string','Neu','callback','[z]=neu(z)');

// EXIT
se_5_2=uicontrol(z.fo,"style","pushbutton", 'Relief','raised','TooltipString','Das Programm verlassen','position', [1300 600 75 20],'string','Exit','callback','close');

//FFT berechnen
se_5_3=uicontrol(z.fo,"style","pushbutton",'Relief','flat','TooltipString','Berechnungen durchführen','position', [1300 525 90 20],'string','FFT berechnen','callback','if z.info(2)==0 then else [z]=rw1(z);[z]=rw2(z);[z]=rw3(z); end');//fft berechnen

//Bandfilter
//High
se_6_1=uicontrol(z.fo,"style","slider",'Relief','flat','min',1,'max',20000,'SnapToTicks','on','sliderstep',[1 1],'value',20000,'TooltipString','Bandpass obere Grenzfrequenz einstellen','position', [60 50 1230 10],'BackgroundColor',[1 0.627 0.478],'callback','z.high=se_6_1.value;set(se_6_2,''string'',string(se_6_1.value/10));[z]=malenmax(z);'); //high slider
se_6_2=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'TooltipString','Bandpass obere Grenzfrequenz vorwahl','position', [1300 50 35 15],'string','2000.0'); // high anzeige vorwahl
se_6_3=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'TooltipString','Bandpass obere Grenzfrequenz','position', [1345 50 35 15],'string','0'); // high anzeige filter
se_6_4=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'position', [1380 50 10 15],'string','H'); // farbmarker
/////////////////////////////////
//Low
se_6_5=uicontrol(z.fo,"style","slider",'Relief','flat','min',1,'max',20000,'SnapToTicks','on','sliderstep',[1 1],'value',1,'TooltipString','Bandpass untere Grenzfrequenz einstellen','position', [60 35 1230 10],'BackgroundColor',[0.678 1 0.183],'callback','z.low=se_6_5.value;set(se_6_6,''string'',string(se_6_5.value/10));[z]=malenmin(z);'); // low slider
se_6_6=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'TooltipString','Bandpass unter Grenzfrequenz vorwahl','position', [1300 35 35 15],'string','0.1'); // low anzeige vorwahl
se_6_7=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'TooltipString','Bandpass untere Grenzfrequenz','position', [1345 35 35 15],'string','0'); // low anzeige filter
se_6_8=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'position', [1380 35 10 15],'string','L'); // farbmarker
//
se_1_1x=uicontrol(z.fo,"style","text",'backgroundcolor',[0.627 0.321 0.176],'position',[1391 50 4 15]);// Farbmarkierung 
se_1_2x=uicontrol(z.fo,"style","text",'backgroundcolor',[0.333 0.419 0.184],'position',[1391 35 4 15]);// Farbmarkierung 
se_1_3x=uicontrol(z.fo,"style","text",'backgroundcolor',[1 0.647 0],'position',[1336 50 4 15]);// Farbmarkierung 
se_1_4x=uicontrol(z.fo,"style","text",'backgroundcolor',[0.678 1 0.184],'position',[1336 35 4 15]);// Farbmarkierung 
//
se_6_9=uicontrol(z.fo,"style","pushbutton",'Relief','flat','TooltipString','Bandpass Berechnungen durchführen','position', [1300 10 90 20],'string','BP berechnen','callback','[z]=bp(z);[z]=rw2(z);[z]=rw3(z)');
ts4=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 510 95 5]);// trennstrich grün
//
se_0_0=uicontrol(z.fo,"style","text",'HorizontalAlignment','left' ,'backgroundcolor',[1 0.95 0.85],'TooltipString','Die mit NEU ausgewählte Datei wird angezeigt','position', [60 10 930 20],'string','Keine Datei ausgewählt.'); //
// PLAY
se_7_1=uicontrol(z.fo,"style","pushbutton", 'Relief','flat','TooltipString','Eingang Wav abspielen','position', [1000 10 75 20],'string','Play Eing.','callback','playsnd(z.W1,z.samplerate)');
se_7_2=uicontrol(z.fo,"style","pushbutton", 'Relief','flat','TooltipString','Ausgang, mit BP gefilterte Wav abspielen','position', [1085 10 75 20],'string','Play Ausg.','callback','playsnd(z.W2,z.samplerate)');
// PLAY TRANSPONIERTE
se_7_3=uicontrol(z.fo,"style","pushbutton", 'Relief','flat','TooltipString','Die mit BP gefilterte Wav um 100 Hz versetzt abspielen. Alle Töne werden mit um 100 Hz höher abgespielt.','position', [1170 10 120 20],'string','Play Ausg. Shift 100Hz','callback','playsnd(z.W3,z.samplerate)');
//////////////////////////////////////////////////////////////////////////////
//schwellen amplitude
se_amp_1=uicontrol(z.fo,"style","slider",'Relief','flat','min',0.1,'max',1000,'value',1,'TooltipString','Schwelle einstellen','position', [1290 80 5 380],'BackgroundColor',[0.398 0.8 0.664],'callback','[z]=level_1(z)'); // 
se_amp_2=uicontrol(z.fo,"style","pushbutton",'Relief','flat','TooltipString','Schwelle beim Powerspektrum anwenden. Nur Frequenzen deren Amplitude größer als der Schwellwert sind werden durchgelassen, alle Frequenzen der Amplitude kleiner ist werden gesperrt. So können Grundrauschen und andere kleine Störgeräusche eliminiert werden.','position', [1300 70 90 20],'string','Schwelle Ber.','callback','[z]=level_2(z)');
/////////////////////////////////////////////////////////////UNTERES FENSTERTEIL
///////////////////////////////////////////////////////////////////////////////
// FFT
se_fft=uicontrol(z.fo,"style","checkbox",'HorizontalAlignment','left','TooltipString','FFT Graph Ein / Aus','position', [1300 505 90 15],'string','FFT','value',1,'callback','[z]=sicht3(z)');
se_fft_blau=uicontrol(z.fo,"style","text",'backgroundcolor',[0 0.5625 0.5625],'HorizontalAlignment','left' ,'position', [1390 505 5 15],'string','');

// POWER
se_pow=uicontrol(z.fo,"style","checkbox",'HorizontalAlignment','left','TooltipString','Leistungsspektrum Graph Ein / Aus','position', [1300 485 90 15],'string','Power','value',1,'callback','[z]=sicht4(z)');
se_pow_red=uicontrol(z.fo,"style","text",'backgroundcolor',[0.8125 0 0.8125],'HorizontalAlignment','left' ,'position', [1390 485 5 15],'string','');
ts5=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 550 95 5]);// trennstrich grün


///////////////////////////////////////////////////////////AMPLITUDEN FENSTER 2
se_amp_1000=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 444 90 15],'string','1           0  dB','value',1,'callback','[z]=fensta1000(z)');
se_amp_500=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 424 90 15],'string',',5       -6  dB','value',0,'callback','[z]=fensta500(z)');
se_amp_250=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 404 90 15],'string',',25     -12  dB','value',0,'callback','[z]=fensta250(z)');
se_amp_100=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 384 90 15],'string',',10     -20  dB','value',0,'callback','[z]=fensta100(z)');
se_amp_50=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 364 90 15],'string',',05     -26  dB','value',0,'callback','[z]=fensta50(z)');
se_amp_25=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 344 90 15],'string',',025   -32  dB','value',0,'callback','[z]=fensta25(z)');
se_amp_10=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 324 90 15],'string',',01     -40  dB','value',0,'callback','[z]=fensta10(z)');
se_amp_5=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 304 90 15],'string',',005   -46  dB','value',0,'callback','[z]=fensta5(z)');
se_amp_2=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 284 90 15],'string',',0025 -52  dB','value',0,'callback','[z]=fensta2(z)');
se_amp_01=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2','HorizontalAlignment','left','position', [1300 264 90 15],'string',',001   -60  dB','value',0,'callback','[z]=fensta01(z)');

ts6=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 255 95 5]);// trennstrich grün

///////////////////////////////////////////////////////////FREQUENZEN FENSTER 2
se_fre_2000=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 237 90 15],'string','2.000 Hz','value',1,'callback','se_6_1.max=20000;[z]=fenstf2000(z);');
se_fre_1000=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 217 90 15],'string','1.000 Hz','value',0,'callback','se_6_1.max=10000;[z]=fenstf1000(z)');
se_fre_0500=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 197 90 15],'string','500 Hz','value',0,'callback','se_6_1.max=5000;[z]=fenstf500(z)');
se_fre_0250=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 177 90 15],'string','250 Hz','value',0,'callback','se_6_1.max=2500;[z]=fenstf250(z)');
se_fre_0120=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left' ,'position', [1300 157 90 15],'string','120 Hz','value',0,'callback','se_6_1.max=1200;[z]=fenstf120(z)');
se_fre_060=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 137 90 15],'string','60 Hz','value',0,'callback','se_6_1.max=600;[z]=fenstf60(z)');
se_fre_030=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 117 90 15],'string','30 Hz','value',0,'callback','se_6_1.max=300;[z]=fenstf30(z)');
se_fre_012=uicontrol(z.fo,"style","radiobutton",'groupname','fenster2f','HorizontalAlignment','left','position', [1300 97 90 15],'string','12 Hz','value',0,'callback','se_6_1.max=120;[z]=fenstf12(z)');

ts7=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'position', [1300 90 95 5]);// trennstrich grün

se_pro=uicontrol(z.fo,"style","text",'HorizontalAlignment','right' ,'TooltipString','Das vom BP gefilterte Signal in Prozent vom Eingangssignal.','position', [1300 560 90 15],'string','0 Prozent'); //

//////////////////////////////////////////////////////////////GRAPHEN FENSTER 2

///////////////////////////////////////////////////////////////////////////////////////////////
// KOPFZEILE FENSTER 1
u0a=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'HorizontalAlignment','center' ,'position', [60 880 1335 15],'string','Wav Eingang (blau)  /  Wav Ausgang (rot)');
// KOPFZEILE FENSTER 2
u0b=uicontrol(z.fo,"style","text",'backgroundcolor',[0.1 1 0.8],'HorizontalAlignment','center' ,'position', [60 463 1335 15],'string','Fast Fourier Transformation FFT (grün)  /  Leistungsspektrum (violet). Zeitbasis der Berechnung 10 Sekunden.');
u0c=uicontrol(z.fo,"style","text",'backgroundcolor',[1 1 1],'HorizontalAlignment','center' ,'position', [10 65 45 15],'string','Frequenz'); //frequenzachse

//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////GRAPHIKFENSTER EINBAUEN
z.fen1 = newaxes(); // FENSTER OBEN 
//z.fen1.axes_bounds = [0,0,1.15,0.5];
z.fen1.axes_bounds = [0,-0.01,1.15,0.5];
//
sca(z.fen1)
z.fen1.visible = "on";
z.fen1.axes_visible = ["on","on","on"];
z.fen1.axes_reverse = ["off","off","off"];
z.fen1.auto_ticks = ["off","off","off"];
z.fen1.grid = [1,1];
z.fen1.grid_thickness = [1,1];
z.fen1.grid_style = [1,1];
z.fen1.grid_position = "background";
z.fen1.title.font_size=2;
z.fen1.title.text="";
z.fen1.x_label.font_size=2;
z.fen1.x_label.text="";
z.fen1.x_label.visible="on";
z.fen1.y_label.font_size=2;
z.fen1.y_label.text="Amplitude";
z.fen1.y_label.visible="on";
z.fen1.auto_scale="off";
z.fen1.auto_margins = "off"
z.fen1.margins = [0.04,0.2,0.07,0.1]
// ZEITACHSE
z.fen1.x_ticks=tlist(['locations','labels'],[0:48000:480000],string([0:1:10]));
// AMPLITUDENACHSE
z.fen1.y_ticks=tlist(['locations','labels'],[-1:0.1:1],string([-1:0.1:1]));
z.fen1.sub_tics=[9,4,9];
z.fen1.box = "back_half";
z.fen1.background = -2;
z.fen1.tight_limits = ["on","on"];
z.fen1.cube_scaling = "on";
z.fen1.isoview = "off";
// MIT DEN FOLGENDEN EINSTELLUNGEN DER DREI ACHSEN KANN DIE ANZEIGE ANGEPASST WERDEN 
zh=z.fen1.data_bounds;
zh(1,2)=-1;
zh(2,2)=1;
zh(1,1)=0;
zh(2,1)=480000;
z.fen1.data_bounds =zh;
z.fen1.tag='fenster 1';
/////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////
z.fen2 = newaxes(); // FENSTER UNTEN
//z.fen2.axes_bounds = [0,0.47,1.15,0.5]; // [x_left, y_top, width, height]
z.fen2.axes_bounds = [0,0.44,1.15,0.5];

//

sca(z.fen2)
z.fen2.visible = "on";
z.fen2.axes_visible = ["on","on","on"];
z.fen2.axes_reverse = ["off","off","off"];
z.fen2.auto_ticks = ["off","off","off"];
z.fen2.grid = [1,1];
z.fen2.grid_thickness = [1,1];
z.fen2.grid_style = [1,1];
z.fen2.grid_position = "background";
z.fen2.title.font_size=2;
z.fen2.title.text="";
z.fen2.x_label.font_size=2;
z.fen2.x_label.text="";
z.fen2.x_label.visible="on";
z.fen2.y_label.font_size=2;
z.fen2.y_label.text="Amplitude";
z.fen2.y_label.visible="on";
z.fen2.auto_scale="off";
z.fen2.auto_margins = "off"
z.fen2.margins = [0.04,0.2,0.1,0.07]
//
// FREQUENZACHSE
z.fen2.log_flags='nn';
z.fen2.x_ticks=tlist(['locations','labels'],(0:100:2000),string(0:100:2000));
// AMPLITUDENAACHSE
z.fen2.y_ticks=tlist(['locations','labels'],[0,100,200,300,400,500,600,700,800,900,1000],["0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1"]);
z.fen2.sub_tics=[9,9,9];
z.fen2.box = "back_half";
z.fen2.background = -2;
z.fen2.tight_limits = ["on","on"];
z.fen2.cube_scaling = "on";
z.fen2.isoview = "off";
// MIT DEN FOLGENDEN EINSTELLUNGEN DER DREI ACHSEN KANN DIE ANZEIGE ANGEPASST WERDEN 
//zh=z.fen2.data_bounds;
zh(1,2)=0;
zh(2,2)=1000;
zh(1,1)=0;
zh(2,1)=2000;
z.fen2.data_bounds =zh;
z.fen2.tag='fenster2';
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
[z]=fenstf2000(z)


