To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
The primary repository for this project is hosted at git://github.com/rmeddis/MAP.git .
This repository is a read-only copy which is updated automatically every hour.
root / userProgramsTim / fourierautocorrelationhistogram_direct.m @ 38:c2204b18f4a2
History | View | Annotate | Download (3.42 KB)
| 1 | 38:c2204b18f4a2 | rmeddis | function fach=fourierautocorrelationhistogram_direct(ANpattern,sfreq,plothandle) |
|---|---|---|---|
| 2 | |||
| 3 | |||
| 4 | time_axis = 0:1/sfreq:(size(ANpattern,2)-1)/sfreq; |
||
| 5 | |||
| 6 | %find how many samples of AN_pattern are 10ms and 3ms |
||
| 7 | %one_sample_is_a_time_of = time_axis(2); |
||
| 8 | [tmp, start_time_index] = min(abs(0-time_axis)); |
||
| 9 | [tmp, stop20_time_index] = min(abs(0.020-time_axis)); |
||
| 10 | number_of_samples20ms = stop20_time_index - start_time_index; |
||
| 11 | |||
| 12 | [tmp, stop3_time_index] = min(abs(0.003-time_axis)); |
||
| 13 | number_of_samples3ms = stop3_time_index - start_time_index; |
||
| 14 | every_3ms = 1:number_of_samples3ms:size(ANpattern,2)-number_of_samples20ms; |
||
| 15 | |||
| 16 | hamm_window = hamming(11); |
||
| 17 | halfHamming = (length(hamm_window)-1)/2; |
||
| 18 | |||
| 19 | % window normalization |
||
| 20 | |||
| 21 | norm = conv(ones(1,floor(number_of_samples20ms/2)),hamm_window); |
||
| 22 | norm = norm(5+1:end-5)'; |
||
| 23 | win_size = number_of_samples20ms; |
||
| 24 | half_win_size = floor(win_size/2); |
||
| 25 | hop_size = number_of_samples3ms; |
||
| 26 | |||
| 27 | %preallocation due to speed |
||
| 28 | fach = zeros(half_win_size,size(every_3ms,2)+1); |
||
| 29 | |||
| 30 | for iCounter = 1:size(ANpattern,1) %each channel |
||
| 31 | fprintf('Channel No. %i\n',iCounter);
|
||
| 32 | %time_counter = 1; |
||
| 33 | %for jCounter = every_3ms %every 3ms time segment |
||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | %% Guy's code |
||
| 38 | % enframe this signal |
||
| 39 | |||
| 40 | frames = enframe(ANpattern(iCounter,:),win_size,hop_size); |
||
| 41 | |||
| 42 | % compute the autocorrelation |
||
| 43 | |||
| 44 | %acf = real(ifft(abs(fft(frames,[],2)).^2,[],2)); |
||
| 45 | %acf(acf<0)=0; |
||
| 46 | %acf = sqrt(acf(:,1:half_win_size)); |
||
| 47 | |||
| 48 | % smooth with hamming window and take the root |
||
| 49 | |||
| 50 | for frame=1:size(frames,1) |
||
| 51 | |||
| 52 | |||
| 53 | smoothed_frame = conv(frames(frame,:),hamm_window); |
||
| 54 | smoothed_frame = smoothed_frame(halfHamming+1:end-halfHamming); |
||
| 55 | fsra = 20*log10(abs(fft(smoothed_frame-mean(smoothed_frame)))); |
||
| 56 | fsra = fsra(1:floor(length(fsra)/2)); |
||
| 57 | |||
| 58 | t = [0:1/sfreq:length(smoothed_frame)/sfreq-1/sfreq]; |
||
| 59 | frequency = [0:1/t(end):1/(2*(t(2)-t(1)))]; |
||
| 60 | %identify peaks in the fft |
||
| 61 | df = [0 ; diff(fsra')]; |
||
| 62 | idx = find((df(1:end-1)>=0)&(df(2:end)<0)); |
||
| 63 | % % interpolate |
||
| 64 | % a=df(idx); |
||
| 65 | % b=df(idx+1); |
||
| 66 | % idx = (idx-1+a./(a-b)); |
||
| 67 | [sorted,sortedindex]=sort(fsra(idx),'descend'); |
||
| 68 | % just take the three highest values of the fourier-transform |
||
| 69 | valid_peak_index = sortedindex(1:min([length(sortedindex) 1])); |
||
| 70 | amp = sorted(1:min([length(sortedindex) 1])); |
||
| 71 | |||
| 72 | %store valid peaks according to amplitude in a histogram |
||
| 73 | if (~isempty(valid_peak_index)) |
||
| 74 | for k=1:length(valid_peak_index), |
||
| 75 | fach(idx(valid_peak_index(k)),frame) = fach(idx(valid_peak_index(k)),frame)+amp(k); |
||
| 76 | end |
||
| 77 | end |
||
| 78 | %transform index into frequencies |
||
| 79 | |||
| 80 | end |
||
| 81 | end |
||
| 82 | |||
| 83 | |||
| 84 | %plot the result |
||
| 85 | if ~exist('plothandle'), plothandle=figure; end
|
||
| 86 | maxfrequency = 4000; |
||
| 87 | [tmp,number_of_channels_to_display] = min(abs(frequency-maxfrequency)); |
||
| 88 | frequency = frequency(1:number_of_channels_to_display); |
||
| 89 | |||
| 90 | set(gcf,'Currentaxes',plothandle); |
||
| 91 | |||
| 92 | YTickIdx = 1:floor(numel(frequency)/6):numel(frequency); |
||
| 93 | XTickIdx = 1:floor(numel(every_3ms)/6):numel(every_3ms); |
||
| 94 | YTickIdxRev = numel(frequency)+1-YTickIdx; |
||
| 95 | if ~isempty(gca) |
||
| 96 | axes(gca); %#ok<MAXES> |
||
| 97 | imagesc(fach(1:number_of_channels_to_display,:)); |
||
| 98 | axis xy |
||
| 99 | set(gca, 'YTick', YTickIdx); |
||
| 100 | set(gca, 'YTickLabel', num2str( frequency(YTickIdx)', '%0.0f' )); |
||
| 101 | ylabel('frequency (Hz)')
|
||
| 102 | set(gca, 'XTick', XTickIdx); |
||
| 103 | set(gca, 'XTickLabel', XTickIdx.*3); |
||
| 104 | xlabel('Time (ms)')
|
||
| 105 | end |
||
| 106 | |||
| 107 | |||
| 108 |