MATLAB Answers

How to continuously extend a vector in a loop

3 views (last 30 days)
Allan Prince
Allan Prince on 27 Jan 2021
Commented: Allan Prince on 27 Jan 2021
I have a function which is supposed to generate a series of audible tones of L duration and Fs sample frequency. I want to be able to play back the string of tones after all of the samples for each tone are created.
I have created a loop that generates L*Fs samples of each tone, one tone at a time in a loop. Each time through the loop, I generate L*Fs samples stored in a vector for each tone. As each chunk of L*Fs samples is created, I need to extend the length of the vector so that when all tones have been created, I will have one long vector that I can playback using sound();
I am having difficulty continuously extending the vector each time through the loop. I've searched for simular examples but have not found any that seem to enlighten me on how to make this work. I would be grateful if someone could suggest a way to make this work.
%Generate tones
Fs = 48000; %sample frequency
L = 0.5; %duration
n = 0:1/Fs:L; %sample
index = 1:1:length(n)-1;
% tone frequencies (Hz)
notes = [440 495 550 587 660 733 825];
tones=[];
for ii = 1:1:length(notes)
[n1,y] = generate_sigs(L,notes(ii),Fs)
% how to continuously extend vector y each loop ????
tones = [tones y(index,1):y(index):y(index,2)];
end
sound(tones, Fs);
% ***text of the generate sigs function***
% function [n,x] = generate_sigs(L,f,Fs)
% n = [0:round(L*Fs-1)]/Fs;
% x = sin(2*pi*f*n); %create n samples at specified
% %frequency and duration L

Accepted Answer

David Hill
David Hill on 27 Jan 2021
for ii = 1:1:length(notes)
[n1,y] = generate_sigs(L,notes(ii),Fs)
tones = [tones,y];
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!