Skip to content

Commit

Permalink
Changes to Matlab scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Michalevsky committed Sep 15, 2013
1 parent a2e3699 commit d1c9ddc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
10 changes: 10 additions & 0 deletions match.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function match(audio_file, gyro_rec, gyro_fs)
% gyro_fs - Gyro sampling frequency

% plot_spectrums(gyro_rec, GYRO_FS);

[audio_samples, audio_fs] = audioread(audio_file);
[gyro_timestamps, gyro_samples] = read_samples_file(gyro_rec);

match_samples(audio_samples, audio_fs, gyro_samples, gyro_fs);
end
11 changes: 11 additions & 0 deletions match_samples.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function match_samples(audio_samples, audio_fs, gyro_samples, gyro_fs)
display(length(gyro_samples));

display(audio_fs);
display(length(audio_samples));

audio_resampled = resample(audio_samples, gyro_fs, audio_fs);
display(length(audio_resampled));

plot( conv(gyro_samples(:,1), fliplr(audio_resampled)) );
end
32 changes: 6 additions & 26 deletions merge_samples.m
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
function [merged_timestamps, merged_signal] = merge_samples(timestamps1, samples1, timestamps2, samples2)
merged_timestamps = sort([timestamps1; timestamps2]);
merged_signal = zeros(length(timestamps1) + length(timestamps2), size(samples1, 2));
function [merged_timestamps, merged_signal] = ...
merge_samples(timestamps1, samples1, timestamps2, samples2)
% Merge samples given their timestamps

pos1 = 1;
pos2 = 1;
for pos = 1:length(merged_timestamps)
if pos1 <= length(timestamps1) && pos2 <= length(timestamps2)
if timestamps1(pos1) < timestamps2(pos2)
merged_signal(pos, :) = samples1(pos1, :);
pos1 = pos1 + 1;
else
merged_signal(pos, :) = samples2(pos2, :);
pos2 = pos2 + 1;
end;
else
if pos1 <= length(timestamps1)
merged_signal(pos, :) = samples1(pos1, :);
pos1 = pos1 + 1;
end;

if pos2 <= length(timestamps2)
merged_signal(pos, :) = samples2(pos2, :);
pos2 = pos2 + 1;
end;
end;
end;
[merged_timestamps, ix] = sort([timestamps1(:); timestamps2(:)]);
merged_signal = [samples1(:); samples2(:)];
merged_signal = merged_signal(ix);
end
8 changes: 8 additions & 0 deletions merge_signals.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function [merged_signals] = merge_signals(sig1, sig2, offset1, offset2, n)
% Merge two signals sample-by-sample given the samples and offsets

timestamps1 = offset1:n:offset1+length(sig1)*n - 1;
timestamps2 = offset2:n:offset2+length(sig2)*n - 1;
[~, merged_signals] = merge_samples(timestamps1, sig1, ...
timestamps2, sig2);
end
4 changes: 3 additions & 1 deletion plot_spectrums.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
function plot_spectrums(filename, fs)
nfft = 256;
[~, samples] = read_samples_file(filename);
for k = 1:3
figure;
spectrogram(samples(:, k), 128, 120, fs);
% specgram(samples(:, k), 128, 120);
spectrogram(samples(:, k), hanning(nfft), round(nfft * 0.9), nfft, fs);
end;
end

0 comments on commit d1c9ddc

Please sign in to comment.