-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsniff_cricket.m
67 lines (56 loc) · 1.82 KB
/
sniff_cricket.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
64
65
66
67
% Joseph Clements
% COMPE 490L: Criquit
% Load the reference sample:
refSample = audioread('fallfieldcricket.wav');
refSampleF = fft(refSample);
[m, n] = size(refSample); % size of the ref sample
Fs = 44100; % Sampling frequency:
f = Fs*(0:m/2-1)/(m); % Frequency vector, for plotting FFT (0 to samp frq.)
% rSF2 = (20*log10(abs(refSampleF((1:m/2), 1) / (m))));
rSF2 = abs(refSampleF((1:m/2), 1) / (m)); % Magnitude of fft
% Configure comm port
sniff_len = 20000; % number of samples to get from comm port
nucleo = serial('COM3'); % change to right comm port if necessary
set(nucleo, 'BaudRate', 240000);
samp = ones(1,sniff_len); % Allocate array of needed size (see next lines)
s = whos('samp');
nucleo.InputBufferSize = s.bytes; % size input buffer appropriately
% Ref sample time domain plot
subplot(2,2,1)
plot((1:floor(m/2))/floor(m/2), refSample((1:m/2), 1))
title('Sample Cricket Sound (44KHz)')
xlabel('time (s)')
ylabel('Amplitude')
axis('tight')
% Ref sample frequency domain plot
subplot(2,2,2)
plot(f, rSF2)
title('Sample Cricket Sound (frequency domain)')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
axis('tight')
% Open the comm port object, read data
fopen(nucleo);
samp = fread(nucleo, sniff_len);
[v, b] = size(samp);
% Recorded sample time domain plot
subplot(2,2,3)
plot((1:v)/v, samp)
title('Recorded Cricket Sound (20KHz)')
xlabel('time (s)')
ylabel('Amplitude')
axis('tight')
% Prepare fft of recorded sample
sampF = fft(samp);
fs2 = 20e3; % 20k sampling f
f2 = fs2*(0:v-1)/v;
rSFs = abs(sampF(:, 1)/v);
% Recorded sample frequency domain plot
subplot(2,2,4)
plot(f2, rSFs)
title('Recorded Cricket Sound (Frequency domain)')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
axis('tight')
% clean up (stays open and causes problems if not closed)
fclose(nucleo);