MATLAB Answers

How to plot a semilogx stairs function with x values in the center

8 views (last 30 days)
Ozan Oguz
Ozan Oguz on 6 Sep 2013
Edited: Stephan on 13 Dec 2016
  • I have some acoustic test data.I could'nt find a simple solution for my plotting needs.Here it goes...
  • X axis have frequency values. Each X value shows a center frequency of measured octave interval. Each Y shows the overal sound pressure level at a center freq. for that interval.
  • Assume
Center frequencies as: 32,64,125,250,500 in Hz.
Respectively, sound pressure values are: 90,95,100,93,82 in dBP.
  • Example:
Y value at 125 Hz is 100 dBP, and the stair step centered at 125 Hz must extend from half octave below to half octave above 125 Hz (in this case, we'll also need a value before 32Hz and an other after 500Hz).
(X axis must show center frequencies).
  • So... How to plot this?

Answers (1)

Stephan
Stephan on 12 Dec 2016
Edited: Stephan on 13 Dec 2016
I know it is an old question, but just for documentation purposes, here is my solution:
%center frequencies of the one third octaves
fc = [160 200 250 315 400 500 630 800 1000 1250 1600 2000 2500 3150 4000 5000];
%sound pressure level
level = [10 20 30 40 30 20 10 20 40 50 20 10 20 30 40 20];
%calculate new frequency values, i.e. one half of a one third octave -> 2^(-1/6)
%also: last value has to be doubled to get a proper line
f_low = [fc .* 2^(-1/6), fc(end) * 2^(1/6)];
level = [level, level(end)];
figure;
stairs(f_low, level);
ax = gca;
ax.XScale = 'log'; %make x-axis logarithmic
ax.XTick = fc; %set the tick values to be the center frequencies
ax.XTickLabelRotation = 90; %rotate the x-labels for better visibility
ax.XLim = [fc(1), fc(end)]; %set the limits for the x-axis
xlabel('Frequency in Hz');
ylabel('Sound pressure level in dB(A)');

Community Treasure Hunt

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

Start Hunting!