-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beat.m
65 lines (55 loc) · 1.36 KB
/
Beat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function[X] = Beat(A,B,fc,fdelta,fsamp,dur)
%Beat generate AM or beat notes (sound and plot)
%Usage: This uses the parameters Beat(A,B,fc,fdelta,fsamp,dur)
%The output is X
%check to see if real
aR=isreal(A);
bR=isreal(B);
fcR=isreal(fc);
fdR=isreal(fdelta);
fsamp_int= mod(fsamp,1);
%check fsamp is an integer using modulus (fsamp/1).If is integer returns 0(no rem)
%If is not int, will return some remander. Will test using ~= (not equals)
durR=isreal(dur);
if(aR==0)
error('A must be real');
elseif(bR==0)
error('B must be real');
elseif(fcR==0)
error('fc must be real');
elseif(fdR==0)
error('fdelta must be real');
elseif(fsamp< 0)||(fsamp_int ~= 0)
error('fsamp must be positive and an integer');
elseif(durR==0)
error('dur must be real');
end
tt=0:(1/fsamp):dur;
X1= A*cos(2*pi*(fc-fdelta).*tt);
X2= B*cos(2*pi*(fc+fdelta).*tt);
%must use dot multiplication
X= X1+X2;
subplot(3,1,1)
plot(tt,X1);
xlabel('time (t)');
ylabel('X1');
title(['X1= ',num2str(A),'*cos(2*pi(',num2str(fc),'-', num2str(fdelta) , ').*t)']);
%dont put tt using num2str
xlim([0 dur/10]);
grid on;
subplot(3,1,2)
plot(tt,X2);
xlabel('time (t)');
ylabel('X2');
title(['X2= ',num2str(B),'*cos(2*pi(',num2str(fc),'+',num2str(fdelta),').*t)']);
xlim([0 dur/10]);
grid on;
subplot(3,1,3)
plot(tt,X);
xlabel('time (t)');
ylabel('X');
title('X= X1 + X2');
xlim([0 dur/10]);
grid on;
sound(X,fsamp);
end