Audio Chirp From Point Backwards

Hi,
I would like to be able to create a sine wave starting at a given point (for example at 2 seconds) and from this point backwards to the start a chirp which will be in phase with the sine wave. It does not matter where the chirp is ending. If this isn't directly possible maybe there is a way to create it forwards and inverse the whole construct later. Thanks!

 Accepted Answer

Try this:
numElements = 50000;
% Make time axis.
t = linspace(0, 2, numElements);
% Make signal that starts at 540 Hz and, at 2 seconds, ends at 340 Hz.
y = chirp(t,540, 2,340);
% Flip it and prepend it.
t = linspace(0, 4, 2*numElements);
y = [fliplr(y), y]; % This is the final signal.
% Plot middle to check phase match.
index1 = numElements - 500;
index2 = numElements + 500
plot(t(index1 : index2), y(index1 : index2), 'b-')
grid on;
% Put a line at the mirroring line
xl = mean(t(numElements : numElements+1))
xline(xl, 'Color', 'r')
% Play the chirp sound
sound(y, length(y))

More Answers (1)

Voss
Voss on 27 Dec 2021
If you have the signal processing toolbox, you can use chirp.

4 Comments

How exactly would you do it? I tried chirp but was not able to use it backwards from the 2 sec point.
Here's an example from the documentation that creates a chirp with frequency 0 at time 0 and frequency 250 Hz at time 1 second.
t = 0:1/1e3:2;
y = chirp(t,0,1,250);
If you want, say frequency 0 at time 2 seconds and frequency 250 Hz at time 0, you can do this instead:
t = 0:1/1e3:2;
y = chirp(t,250,2,0);
I just added a picture for better understanding. In this example I need the chirp to start at the 2 seconds point at 540 and end at the 0 second point at more or less 340 Hz. Important is the transition at the 2 seconds point.
I see. So you can use chirp like this:
t = 0:1/1e4:2;
y = chirp(t,340,2,540);
It looks like chirp will always give a signal whose amplitude is 1 at the beginning and end (implicit in the definition of a "swept-frequency cosine signal" perhaps), i.e., an integer number of periods is used. Therefore, the phase-matching is easy: you can create another cosine signal of frequency 540 Hz that lasts another 2 seconds, say, and concatenate it to the end of y (actually dropping off the last sample of y since it'll be in the new signal). The two signals match phases at t=2 because I use t starting at 0 to calculate the second signal.
y = [y(1:end-1) cos(2*pi*540*t)];
and then the time vector for the combined signal would be:
t = [t(1:end-1) t+2];

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!