record series of frequencies into file, then read file on spectrogram

2 views (last 30 days)
Hi, I have a problem that I cannot find a solution to on the forums.
I have a for loop that controls the pitch of a sinusoidal waveform, which then plays. it plays a series of notes.
I need to record the entire audio signal, then plot it on a spectrogram. but i am not sure how to do this.
any ideas?
thanks
the function:
%define function
function tonegen = tone(samp, fref, decay, index)
%take inputs and set them to local variables
i = index;
fs = samp; %sets sampling rate
fref = fref*2^(2*(i)/24);
dec = decay; %determines the time length of the signal
t = (0:round(decay*fs)-1)/fs;
%define signal
tonegen = cos(2*pi*fref*t);
%signal playback
% soundsc(tonegen,fs)
end
%loop that plays through a hexaconic scale starting at A4 (440Hz)
fs = 4000;
wen = 128;
nfft = 1024;
olap = floor(wen/2);
wn = hamming(wen);
fref = 256;
t = (0:1);
loop that plays through a hexaconic scale starting at A4 (440Hz)
for j=0:23 %sets the range from 0 (reference) to 6 (scale completion)
sig = tone(fs,256,0.1,j) %calls the signal generation function
pause(0.25) %spaces the notes out to prevent overlap
record(audio,5);
soundsc(sig,fs);
end
testsig = cos(2*pi*fref*t);
[b,faxis,taxis] = spectrogram(testsig,wn,olap,nfft,fs);
figure(1); imagesc(taxis, faxis, abs(b)) % Plot spectrogram
axis('xy') % Flip y axis to put zero Hz on bottom

Answers (1)

Mathieu NOE
Mathieu NOE on 10 Oct 2023
hello my friend
here you are
I simplified a bit your function (ne need to create extra variables that we don't use anyway) and to solve your main problem, I simply created a concatenation of your individual tones plus the 0.25s silence in between
%loop that plays through a hexaconic scale starting at A4 (440Hz)
fs = 4000;
fref = 256;
t = (0:1);
testsig = []; % this will be your final signal including all tones + silences
%loop that plays through a hexaconic scale starting at A4 (440Hz)
for k=0:23 %sets the range from 0 (reference) to 6 (scale completion)
sig = tone(fs,256,0.1,k) %calls the signal generation function
% pause(0.25) %spaces the notes out to prevent overlap
% soundsc(sig,fs);
testsig = [testsig; zeros(round(0.25*fs),1);sig]; % concatenate signal (sig) + silence of 0.25 s
end
soundsc(testsig,fs);
% spectrogram
wen = 128;
nfft = 1024;
olap = floor(wen/2);
wn = hamming(wen);
[b,faxis,taxis] = spectrogram(testsig,wn,olap,nfft,fs);
figure(1); imagesc(taxis, faxis, abs(b)) % Plot spectrogram
axis('xy') % Flip y axis to put zero Hz on bottom
%define function
function tonegen = tone(fs, fref, decay, index)
%take inputs and set them to local variables
fref = fref*2^(2*(index)/24);
t = (0:round(decay*fs)-1)'/fs;
%define signal
tonegen = sin(2*pi*fref*t);
end

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!