Generate pure tone sequence in frequence domain
14 views (last 30 days)
Show older comments
Hi, i'm trying to generate a sequence of pure tones in the frequency domain and later on, convert to the time domain and execute the sound. The frequencies are 1000 Hz, 2000 Hz, 3000 Hz, 4000 Hz and 5000 Hz. I already managed to do this using only time domain, but i am having a very hard time to do this in the frequency domain. Can someone help me ?
1 Comment
Accepted Answer
Scott MacKenzie
on 25 Oct 2021
Edited: Scott MacKenzie
on 25 Oct 2021
Seems you want to start by specifying your signals in the frequency domain, then convert to the time domain. I think this does the trick:
% system spec's (adjust as needed)
sRate = 16584; % sampling rate
sPeriod = 1 / sRate;
n1 = sRate/2; % number of samples in one-sided freq spectrum
% create spectrum in frequency domain
fOneSide = (1:n1)';
specOneSide = zeros(n1,1);
% add sinusoids with varying amplitudes
specOneSide(1000) = 1.0;
specOneSide(2000) = 0.5;
specOneSide(3000) = 0.4;
specOneSide(4000) = 0.6;
specOneSide(5000) = 0.3;
% plot the frequency spectrum
figure;
set(gcf, 'color', 'w', 'units', 'normalized', 'Position', [.2 .4 .6 .4]);
tiledlayout(1,3);
nexttile;
plot(fOneSide,specOneSide);
title('One-sided Frequency Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude Spectrum');
% create the two-sided frequency spectrum
specTwoSide = [flip(specOneSide(1:end)); specOneSide(2:end-1)];
n2 = length(specTwoSide);
f = (-sRate/2:sRate/n2:sRate/2-sRate/n2)';
% plot it
nexttile;
plot(f, specTwoSide);
title('Two-sided Frequency Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude Spectrum');
% ifft the two-sided frequency spectrum to get signals in the time domain
y = ifft(fftshift(specTwoSide));
t = (0:n2-1) / sRate;
% plot it
nexttile;
plot(y(1:100)); % part of the signal only (better visual)
title('Time Domain Signal');
xlabel('Time');
ylabel('Amplitude');
% play it
soundsc(y, sRate);
6 Comments
Scott MacKenzie
on 26 Oct 2021
Yes, and as an example, if you add
n1 = 10*n1;
after the initialization of n1, you are increasing the size of the spectrum by a factor or 10. So, with this, each frequency is specified x10. To get 326.6 Hz, use
specOneSide(3266) = 1.0; % frequency = 326.6 Hz, amplitude = 1
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!