0% found this document useful (0 votes)
45 views

Period Tracking Using Autocorrelation - Dadorran

This document describes using autocorrelation to track the fundamental period of speech signals for the purpose of a YouTube demonstration video. It provides Matlab code to analyze sample speech data by extracting frames, computing the autocorrelation of each frame, and identifying the lag corresponding to the maximum correlation as the estimated period. It then synthesizes test signals and adds noise to illustrate how autocorrelation can identify the fundamental period from periodic and noisy signals but may struggle with signals where one frequency is much stronger than others.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Period Tracking Using Autocorrelation - Dadorran

This document describes using autocorrelation to track the fundamental period of speech signals for the purpose of a YouTube demonstration video. It provides Matlab code to analyze sample speech data by extracting frames, computing the autocorrelation of each frame, and identifying the lag corresponding to the maximum correlation as the estimated period. It then synthesizes test signals and adds noise to illustrate how autocorrelation can identify the fundamental period from periodic and noisy signals but may struggle with signals where one frequency is much stronger than others.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.

html

placeholder for code and links

Home
audio processing
matlab code
pic18f
pic18f bootloader
youtube demo code

Home > matlab code, youtube demo code > pitch/period tracking using autocorrelation

September 24, 2014 dadorran Leave a comment Go to comments

1 %% Using Autocorrelation to track the local period of a sig
2 % This code is used as part of a youtube video demonstratio
3 % See http://youtube.com/ddorran
4 %
5 % Code available at https://dadorran.wordpress.com      
6 %
7 % The following wav file can be downloaded from 
8 %       https://www.dropbox.com/s/3y25abf1xuqpizj/speech_de
9 %% speech analysis example
10
11 [ip fs] = wavread('speech_demo.wav');
12 max_expected_period = round(1/50*fs);
13 min_expected_period = round(1/200*fs);
14 frame_len = 2*max_expected_period;
15
16 for k = 1 : length(ip)/frame_len ‐1;
17 range = (k‐1)*frame_len + 1:k*frame_len;
18 frame = ip(range);
19
20 %show the input in blue and the selected frame in red
21 plot(ip);
22 set(gca, 'xtick',[],'position',[ 0.05  0.82   0.91  0.1
23 hold on;
24 temp_sig = ones(size(ip))*NaN;
25 temp_sig(range) = frame;
26 plot(temp_sig,'r');
27 hold off
28
29 %use xcorr to determine the local period of the frame
30 [rxx lag] = xcorr(frame, frame);
31 subplot(3,1,3)
32 plot(lag, rxx,'r')

1 of 7 10/12/2018 23:11
pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.html

33 rxx(find(rxx < 0)) = 0; %set any negative correlation v
34 center_peak_width = find(rxx(frame_len:end) == 0 ,1); %
35 %center of rxx is located at length(frame)+1
36 rxx(frame_len‐center_peak_width : frame_len+center_peak
37 %     hold on
38 %     plot(lag, rxx,'g');
39 %     hold off
40 [max_val loc] = max(rxx);
41 period = abs(loc ‐ length(frame)+1); 
42
43 title(['Period estimate = ' num2str(period) 'samples ('
44 set(gca, 'position', [ 0.05  0.07    0.91  0.25])
45
46 [max_val max_loc] = max(frame);
47 num_cycles_in_frame = ceil(frame_len/period);
48 test_start_positions = max_loc‐(period*[‐num_cycles_in_
49 index = find(test_start_positions > 0,1, 'last');
50 start_position = test_start_positions(index);
51 colours = 'rg';
52
53 subplot(3,1,2)
54 plot(frame);
55
56 set(gca, 'position',[ 0.05 0.47 0.91 0.33])
57 pause
58 for g = 1 : num_cycles_in_frame
59 if(start_position+period*(g) <= frame_len && period
60 cycle_seg = ones(1, frame_len)*NaN;
61 cycle_seg(start_position+period*(g‐1):start_pos
62 frame(start_position+period*(g‐
63 hold on
64
65 plot(cycle_seg,colours(mod(g, length(colours))+
66 hold off
67 end
68 end
69 pause
70 end
71
72 %% synthesise a periodic signal to use as a basic demo
73 fs = 500;
74 T = 1/fs;
75 N = 250; % desired length of signal
76 t = [0:N‐1]*T; %time vector 
77 f1 = 8; f2=f1*2; 
78 x = sin(2*pi*f1*t‐pi/2) + sin(2*pi*f2*t);
79 plot(t, x)
80 ylabel('Amplitude')
81 xlabel('Time (seconds)')
82 title('Synthesised Signal');
83
84 %% Determine the autocorrelation function
85 [rxx lags] = xcorr(x,x);
86 figure

2 of 7 10/12/2018 23:11
pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.html

87 plot(lags, rxx)
88 xlabel('Lag')
89 ylabel('Correlation Measure')
90 title('Auto‐correlation Function')
91
92 %% Illustrate the auto correlation process
93 %function available from https://dadorran.wordpress.com
94 illustrate_xcorr(x,x) 
95
96 %% Identify most prominent peaks
97 % Most prominent peak will be at the center of the correlat
98 first_peak_loc = length(x) + 1;
99
100 % Lots of possible ways to identify second prominent peak. 
101 % relying on some assumed prior knowledge of the signal. Am
102 % that the signal has a minimum possible period of .06 seco
103 min_period_in_samples = 30; 
104 half_min = min_period_in_samples/2 ;
105
106 seq = rxx;
107 seq(first_peak_loc‐half_min: first_peak_loc+half_min) = min
108 plot(rxx,'rx');
109 hold on
110 plot(seq)
111
112 [max_val second_peak_loc] = max(seq);
113 period_in_samples =  abs(second_peak_loc ‐first_peak_loc)
114 period = period_in_samples*T
115 fundamental_frequency = 1/period
116
117 %% Autocorrelation of a noisy signal 
118 x2 = x + randn(1, length(x))*0.2;
119 plot(x2)
120 ylabel('Amplitude')
121 xlabel('Time (seconds)')
122 title('Noisy Synthesised Signal');
123
124 [rxx2 lags] = xcorr(x2,x2);
125 figure
126 plot(lags, rxx2)
127 xlabel('Lag')
128 ylabel('Correlation Measure')
129 title('Auto‐correlation Function')
130
131 %% Autocorrelation technique can be problematic!
132 % Consider the following signal
133 f1 = 8; f2=f1*2; 
134 x3 = sin(2*pi*f1*t) + 5*sin(2*pi*f2*t);
135 plot(t, x3)
136 ylabel('Amplitude')
137 xlabel('Time (seconds)')
138 title('Synthesised Signal');
139
140 [rxx3 lags] = xcorr(x3,x3,'unbiased');

3 of 7 10/12/2018 23:11
pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.html

141 figure
142 plot(lags, rxx3)
143 xlabel('Lag')
144 ylabel('Correlation Measure')
145 title('Auto‐correlation Function')
146
147 seq = rxx3;
148 seq(first_peak_loc‐half_min: first_peak_loc+half_min) = min
149 plot(seq)
150
151 [max_val second_peak_loc] = max(seq);
152 period_in_samples =  abs(second_peak_loc ‐first_peak_loc)

Report this ad

Report this ad

Share this:

Be the first to like this.

4 of 7 10/12/2018 23:11
pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.html

Related

Frequency Analysis using the Filter Design Using Matlab DFT Windowing
DFT - A practical example Demo

Categories: matlab code, youtube demo code


Comments (0) Trackbacks (0) Leave a comment Trackback

1. No comments yet.

1. No trackbacks yet.

Leave a Reply

Linear Phase Filters – why they are used illustrate_xcorr – code for cross
correlation demos
RSS feed

Blog Stats

257,013 hits

Recent Posts

DSP Foundations Notes


interpLTspice
runLTspice demo
runLTspice.m
locate_peaks matlab function

Archives

5 of 7 10/12/2018 23:11
pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.html

January 2015
November 2014
October 2014
September 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
October 2013
September 2013
August 2013
May 2013
February 2013
January 2013
November 2012
September 2012
April 2012
March 2012

Categories

audio processing
matlab code
pic18f
pic18f bootloader
Uncategorized
youtube demo code

Meta

Register
Log in
Entries RSS
Comments RSS
WordPress.com

6 of 7 10/12/2018 23:11
pitch/period tracking using autocorrelation | dadorran file:///E:/pitch_period tracking using autocorrelation dadorran.html

Report this ad

Top
Create a free website or blog at WordPress.com.

7 of 7 10/12/2018 23:11

You might also like