Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6137497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:44:02+00:00 2026-05-23T17:44:02+00:00

So I wrote this, basically to update the spectrogram in real time. function spec(Fs,

  • 0

So I wrote this, basically to update the spectrogram in real time.

function spec(Fs, n_bits, n_channels, update_rate)
%# Initialise default parameters if not supplied
if (~exist('Fs', 'var'))
    Fs          = 44000;
end
if (~exist('n_bits', 'var'))
    n_bits      = 16;
end
if (~exist('n_channels', 'var'))
    n_channels  = 2;
end
if (~exist('update_rate', 'var'))
    update_rate = 5;
end
plot_colors = hsv(n_channels);

%# Initialise plots, one above each other in a single figure window
figure;

%# Time Domain plot
hold on

%# Setup the audiorecorder which will acquire data off default soundcard
audio_recorder = audiorecorder(Fs, n_bits, n_channels);

set(audio_recorder, 'TimerFcn', {@audioRecorderTimerCallback, ...
                                  audio_recorder});
set(audio_recorder, 'TimerPeriod', 1/update_rate);
set(audio_recorder, 'BufferLength', 1/update_rate);

%# Start the recorder
record(audio_recorder);

end

function audioRecorderTimerCallback(obj, event, audio_recorder)

Fs           = get(obj, 'SampleRate');
num_channels = get(obj, 'NumberOfChannels');
num_bits     = get(obj, 'BitsPerSample');

try
    if (num_bits == 8)
        data_format = 'int8';
    elseif (num_bits == 16)
        data_format = 'int16';
    elseif (num_bits == 32)
        data_format = 'double';
    else
        error('Unsupported sample size of %d bits', num_bits);
    end

    %# stop the recorder, grab the data, restart the recorder. May miss some data
    stop(obj);
    data = getaudiodata(obj, data_format);

    record(obj);

    if (size(data, 2) ~= num_channels)
        error('Soundcard does not support acquisition of %d channels', ...
              length(num_channels))
    end
    data_fft    = fft(double(data));        
    specgram(data_fft,512);                
catch
    %# Stop the recorder and exit
    stop(obj)
    rethrow(lasterror)
end    
drawnow;
end

I always end up with an empty recorder.

I don’t understand why there is this problem, I did record first.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:44:03+00:00Added an answer on May 23, 2026 at 5:44 pm

    The problem is that the spec function doesn’t return anything, so the audio that you’ve recorded is discarded when the function exits.

    Change the function signature to

    function audio_recorder = spec(Fs, n_bits, n_channels, update_rate)
    

    One minor style point is that you can set default values for functions much more cleanly with (cue shameless self-promotion) SetDefaultValue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get the unread count of google reader items. function
I wrote this function to get a pseudo random float between 0 .. 1
Ok this is sort of hard to explain but basically i wrote a PHP
Basically, I want to do this: update vehicles_vehicle v join shipments_shipment s on v.shipment_id=s.id
I know that in C#, if you write ~MyClass() , this basically translates to
I wrote this snippet of code and I assume len is tail-recursive, but a
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this method to check if a page exists or not: protected bool

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.