1 | function fullCodeWave=flyCommand(code,space,audioFileName,sps,repeatTime,AndroidOriOS,flipOrNot) |
---|
2 | |
---|
3 | % code: array of 1s and 0s. 1 means bit "1", 0 means bit "0". |
---|
4 | % audioFileName: like 'audio.wav'. |
---|
5 | % space: space between codes [samples] |
---|
6 | bps = 16; % bits per sample |
---|
7 | %sps = 96000; % sample rate [samples/s] |
---|
8 | |
---|
9 | % 4 demo codes here for testing. you can either put a matrix as the first parameter, or just use 'demo1' ~ 'demo4' for testing purpose. |
---|
10 | if length(code)==5 |
---|
11 | if code=='demo1' |
---|
12 | code=[1 0 1 1 0 1 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 0];%minimum throttle to make propeller rotating |
---|
13 | elseif code=='demo2' |
---|
14 | code=[1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 0 1 0 0 1 1 1 1 1 0 0 0 0];%maximum throttle |
---|
15 | |
---|
16 | elseif code=='demo3' |
---|
17 | code=[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 1 1 0];%minimum throttle |
---|
18 | elseif code=='demo4' |
---|
19 | code=[1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 ];%test for Untitled 04 |
---|
20 | endif |
---|
21 | |
---|
22 | endif |
---|
23 | |
---|
24 | if (length(AndroidOriOS)==3)%'iOS' |
---|
25 | longHIGH=875;longHIGH_s=floor(longHIGH*sps/1e6)+1;%longHIGH[us] longHIGH_s[sample] |
---|
26 | longLOW=729;longLOW_s=floor(longLOW*sps/1e6); |
---|
27 | shortHIGH=458;shortHIGH_s=floor(shortHIGH*sps/1e6)+1; |
---|
28 | shortLOW=333;shortLOW_s=floor(shortLOW*sps/1e6); |
---|
29 | |
---|
30 | codeWave=[halfSineGenDirect('u',longHIGH_s,0,1,sps);zeros(shortLOW_s,1);halfSineGenDirect('u',longHIGH_s,0,1,sps);zeros(shortLOW_s,1)]; %starting 5 half period |
---|
31 | |
---|
32 | % notice that the "code" here should be a 28 bit binary array. |
---|
33 | for i=1:size(code,2) |
---|
34 | codeWave = [codeWave; bitGenHDMI(code(i),1,sps,longHIGH_s,longLOW_s,shortHIGH_s,shortLOW_s)]; |
---|
35 | endfor |
---|
36 | |
---|
37 | codeWave=[codeWave; halfSineGenDirect('u',longHIGH_s,0,1,sps)]; %last half period |
---|
38 | |
---|
39 | if(flipOrNot=='flip') |
---|
40 | codeWave=-codeWave; |
---|
41 | endif |
---|
42 | |
---|
43 | fullCodeWave=[]; |
---|
44 | for i=1:repeatTime |
---|
45 | fullCodeWave=[fullCodeWave; codeWave; zeros(space,1)]; |
---|
46 | endfor |
---|
47 | |
---|
48 | |
---|
49 | %fullCodeWave=[fullCodeWave zeros(sizeof(fullCodeWave))]; |
---|
50 | |
---|
51 | elseif length(AndroidOriOS)==7 %'Android' |
---|
52 | |
---|
53 | % number with _s suffix is in samples. Optimized with 44100 sampling rate. |
---|
54 | longHIGH=875;longHIGH_s=floor(longHIGH*sps/1e6)-3;%longHIGH[us] longHIGH_s[sample] |
---|
55 | longLOW=729;longLOW_s=floor(longLOW*sps/1e6+3)+1; |
---|
56 | shortHIGH=458;shortHIGH_s=floor(shortHIGH*sps/1e6)-3; |
---|
57 | shortLOW=333;shortLOW_s=floor(shortLOW*sps/1e6)+1+2; |
---|
58 | |
---|
59 | shortLOW_ss=floor(shortLOW*sps/1e6)+2; |
---|
60 | |
---|
61 | |
---|
62 | % PAY ATTENTION to the special starting and ending code of command. |
---|
63 | codeWave=[halfSineGenDirect('d',longLOW_s-1,0,1,sps);halfSineGenDirect('u',longHIGH_s-2,0,1,sps); halfSineGenDirect('d',shortLOW_ss+2,0,1,sps);halfSineGenDirect('u',longHIGH_s-2,0,1,sps); halfSineGenDirect('d',shortLOW_ss+3,0,1,sps)]; %starting 5 half period |
---|
64 | |
---|
65 | % notice that the "code" here should be a 28 bit binary array. |
---|
66 | % function bitGenDirect will generate a complete digit in the form of sine wave of either "0" or "1" |
---|
67 | for i=1:size(code,2) |
---|
68 | codeWave = [codeWave; bitGenDirect(code(i),1,sps,longHIGH_s, longLOW_s,shortHIGH_s,shortLOW_s)]; |
---|
69 | |
---|
70 | endfor |
---|
71 | |
---|
72 | |
---|
73 | codeWave=[codeWave; halfSineGenDirect('u',longHIGH_s,0,1,sps)]; %last half period |
---|
74 | |
---|
75 | if(flipOrNot=='flip') |
---|
76 | codeWave=-codeWave; |
---|
77 | endif |
---|
78 | |
---|
79 | fullCodeWave=[]; |
---|
80 | for i=1:repeatTime |
---|
81 | fullCodeWave=[fullCodeWave; codeWave; zeros(space,1)]; |
---|
82 | endfor |
---|
83 | |
---|
84 | |
---|
85 | endif %if AndroidOriOS=='iOS' |
---|
86 | |
---|
87 | wavwrite(fullCodeWave, sps, bps, audioFileName); |
---|