How to change the spectrogram magnitude to linear? (2024)

46 views (last 30 days)

Show older comments

Kalasagarreddi Kottakota on 13 Jun 2024 at 10:52

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear

Commented: Star Strider on 13 Jun 2024 at 13:04

Open in MATLAB Online

I have the following code of 100 Hz tone with a magnitude of 2. But on the colorbar I get magnitude as 110. Could you help me to resolve this?

clear all; clc;

% Sample signal

fs = 1000; % Sampling frequency (Hz)

t = 0:1/fs:2; % Time vector

x = 2*cos(2*pi*100*t); % Example signal

% Compute the spectrogram

window = hann(256); % Window function

noverlap = 128; % Number of overlapping samples

nfft = 256; % Number of FFT points

[S, F, T] = spectrogram(x, window, noverlap, nfft, fs);

S_magnitude = abs(S); % Magnitude of the spectrogram

% Plot the spectrogram

figure;

imagesc(T, F, S_magnitude);

axis xy;

xlabel('Time (s)');

ylabel('Frequency (Hz)');

title('Spectrogram (Linear Scale)');

colorbar;

How to change the spectrogram magnitude to linear? (2)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

sai charan sampara on 13 Jun 2024 at 11:51

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#answer_1471506

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#answer_1471506

Open in MATLAB Online

Hello,

The "spectrogram" function in MATLAB returns the Short-Time Fourier Transform(STFT) of the input signal. It is used to analyze how the frequency content of a signal changes over time. It gives the variation of signal in the frequency domain over time. In this case, since the signal is a constant frequency signal the spectrogram is a straight line with the maximum value at 100 Hz. Which is the frequency of the signal "x". The "colorbar" varies up to 110 because the maximum value of "S_magnitude" is around 110. As shown in the code below the maximum value of "S_magnitude" for different time values is 114.97 and from the "F" vector we can see that it occurs at around 100Hz frequency.

clear all; clc;

% Sample signal

fs = 1000; % Sampling frequency (Hz)

t = 0:1/fs:2; % Time vector

x = 2*cos(2*pi*100*t); % Example signal

% Compute the spectrogram

window = hann(256); % Window function

noverlap = 128; % Number of overlapping samples

nfft = 256; % Number of FFT points

[S, F, T] = spectrogram(x, window, noverlap, nfft, fs);

S_magnitude = abs(S); % Magnitude of the spectrogram

% Plot the spectrogram

figure;

imagesc(T, F, S_magnitude);

axis xy;

xlabel('Time (s)');

ylabel('Frequency (Hz)');

title('Spectrogram (Linear Scale)');

colorbar;

How to change the spectrogram magnitude to linear? (4)

[val,idx]=max(S_magnitude)

val = 1x14

114.9696 114.9701 114.9698 114.9698 114.9701 114.9696 114.9701 114.9698 114.9698 114.9701 114.9696 114.9701 114.9698 114.9698

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

idx = 1x14

27 27 27 27 27 27 27 27 27 27 27 27 27 27

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

F(idx(1))

ans = 101.5625

1 Comment

Show -1 older commentsHide -1 older comments

Kalasagarreddi Kottakota on 13 Jun 2024 at 11:59

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#comment_3185836

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#comment_3185836

Hi @sai charan sampara, the problem is about the wrong magnitude it is showing. When you apply an stft, the magnitude of the color bar should be the strength of the signal. Here the strength of the signal is 2 units. So, I expect the colorbar to stand maximum at 2 units. But it is 115. Dont know what exactly the output of spectrogram function.

Sign in to comment.

Star Strider on 13 Jun 2024 at 12:24

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#answer_1471526

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#answer_1471526

Open in MATLAB Online

I susp[ect that you may actually want to use the pspectrum function with the 'spectrogram' type. To understand the differences between them, see the spectrogram documentation section on Compare spectrogram and pspectrum Functions. (The principal differences are the units of the output.)

I changed the output plot to surfc to make this a bit more understandable. Change it back if you like, or use

view(0,90)

with the surfc plot.

Try this —

clear all; clc;

% Sample signal

fs = 1000; % Sampling frequency (Hz)

t = 0:1/fs:2; % Time vector

x = 2*cos(2*pi*100*t); % Example signal

% Compute the spectrogram

window = hann(256); % Window function

noverlap = 128; % Number of overlapping samples

nfft = 256; % Number of FFT points

% [S, F, T] = spectrogram(x, window, noverlap, nfft, fs);

[S, F, T] = pspectrum(x, fs, 'spectrogram', 'OverlapPercent',50);

S_magnitude = abs(S); % Magnitude of the spectrogram

% Plot the spectrogram

figure;

% imagesc(T, F, S_magnitude);

surfc(T, F, S_magnitude, 'EdgeColor','interp')

% axis xy;

xlabel('Time (s)');

ylabel('Frequency (Hz)');

title('Spectrogram (Linear Scale)');

colorbar;

zlim([0 2])

How to change the spectrogram magnitude to linear? (7)

% view(0,90)

.

2 Comments

Show NoneHide None

Kalasagarreddi Kottakota on 13 Jun 2024 at 12:35

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#comment_3185861

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#comment_3185861

Edited: Kalasagarreddi Kottakota on 13 Jun 2024 at 12:46

Open in MATLAB Online

Hi @Star Strider, I changed the magnitude of signal x to 5 and expecting the same in the spectrogram, but it turned to max as 12. I am wondering why this change. I am basically looking for STFT of the signal whose color bar directly shows the same magnitude of the signal.

clear all ; clc;

% Sample signal

fs = 1000; % Sampling frequency (Hz)

t = 0:1/fs:2; % Time vector

x = 5*cos(2*pi*100*t); % Example signal%-----------------------chnage

% Compute the spectrogram

window = hann(256); %Window function

noverlap = 128; % Number of overlapping samples

nfft = 256; % Number of FFT points

% [S, F, T] = spectrogram(x, window, noverlap, nfft, fs);

[S, F, T] = pspectrum(x, fs, 'spectrogram' , 'OverlapPercent' ,50);

S_magnitude = abs(S); % Magnitude of the spectrogram

% Plot the spectrogram

figure;

% imagesc(T, F, S_magnitude);

surfc(T, F, S_magnitude, 'EdgeColor' , 'interp' )

% axis xy;

xlabel( 'Time(s)' );

ylabel( 'Frequency(Hz)' );

title( 'Spectrogram (Linear Scale)' );

colorbar;

How to change the spectrogram magnitude to linear? (9)

% zlim([0 2])

Star Strider on 13 Jun 2024 at 13:04

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#comment_3185891

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2128271-how-to-change-the-spectrogram-magnitude-to-linear#comment_3185891

I read through the documentation, including for the periodogram function. It seems that it should produce the power spectrum, not the power spectral density, however taking the square root of ‘S’ still does not give the desired result. Maybe one of the MATLAB staff can explain this. I’m obviously overlooking something.

Sign in to comment.

Sign in to answer this question.

See Also

Tags

  • signal processing
  • spectrogram

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to change the spectrogram magnitude to linear? (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How to change the spectrogram magnitude to linear? (2024)

FAQs

How is a spectrogram calculated? ›

Spectrograms are normally computed using the Short-Time Fourier Transform (STFT), and can be done in real-time. It consists of splitting the signal into overlapped frames, using a window function (typically scaled cosine like Hann) and then computing the FFT for the frame.

What is the unit of a spectrogram? ›

With "pspectrum" the unit is expressed as db and with "spectrogram" the unit is expressed as db/Hz.

What is the difference between spectrogram and STFT in Matlab? ›

stft focuses on the FT of windowed and segmented (overlaped) data and the output can be used to reconstruct the original (under certain condition). spectrogram focuses on the spectral estimation based on STFT.

What is a magnitude spectrogram? ›

The magnitude spectrogram is computed by framing the signal into short time windows, applying a hamming (or similar) window, computing the fft over each window, and taking the absolute value of the complex numbers obtained.

What are the 3 parameters of a spectrogram? ›

Bandwidth The bandwidth depends on all three parameters (FFT length, Frame size, Window) and the sampling frequency of the sound file.

How to interpret spectrograms? ›

In the spectrogram view, the vertical axis displays frequency in Hertz, the horizontal axis represents time (just like the waveform display), and amplitude is represented by brightness. The black background is silence, while the bright orange curve is the sine wave moving up in pitch.

What are the dimensions of a spectrogram? ›

In a spectrogram, the horizontal dimension represents time and the vertical dimension represents frequency.

What is spectrogram intensity? ›

A spectrogram uses color to show the intensity (or loudness) of sound at various frequencies (or pitches) over time. Vertically you can see whether sound is more or less intense at different frequencies. Lower on the vertical axis are lower frequencies, higher on the vertical axis are higher frequencies.

What is the difference between a waveform and a spectrogram? ›

A waveform tracks changes in air pressure over time as a sound is produced. The bottom display shows a spectrogram. A spectrogram provides information about the acoustic components of a sound. (We will learn about these acoustic components in more detail throughout the semester.)

What is an example of a spectrogram? ›

A spectrogram illustrates how the constituent frequencies of a signal vary over time. This application generates the spectrogram of several audio files, including a DTMS tone, human voice saying “MapleSim”, violin note played with vibrato, and an entire violin scale, C8 piano note, series of dolphin clicks, and more.

How to normalize spectrogram in MATLAB? ›

The normalized spectrogram function is defined by:
  1. [matrix, matrix_t, matrix_f] = norm_spectrogram(t, acc)
  2. plot_norm_matrix(m, mt, mf, t, acc, regname)
  3. % Load the data data = load('data/CNV_APED_201604162359_N_100.txt'); % Set time and acceleration array t = data(:, 1); acc = data(:, 2) ./ 980; % Convert from cm/s2 to g.

What is the relationship between spectrogram and STFT? ›

s = spectrogram( x ) returns the Short-Time Fourier Transform (STFT) of the input signal x . Each column of s contains an estimate of the short-term, time-localized frequency content of x . The magnitude squared of s is known as the spectrogram time-frequency representation of x [1].

What is the code for spectrogram in MATLAB? ›

s = spectrogram( x , window ) uses window to divide the signal into segments and perform windowing. s = spectrogram( x , window , noverlap ) uses noverlap samples of overlap between adjoining segments. s = spectrogram( x , window , noverlap , nfft ) uses nfft sampling points to calculate the discrete Fourier transform.

How is a spectrogram generated? ›

A spectrogram can be generated by an optical spectrometer, a bank of band-pass filters, by Fourier transform or by a wavelet transform (in which case it is also known as a scaleogram or scalogram).

How do you interpret a spectrogram? ›

In the spectrogram view, the vertical axis displays frequency in Hertz, the horizontal axis represents time (just like the waveform display), and amplitude is represented by brightness. The black background is silence, while the bright orange curve is the sine wave moving up in pitch.

What is the spectrogram analysis method? ›

To generate a spectrogram, a speech signal is divided into frames which are multiplied by a Hamming window as shown in Fig. 6. Each windowed frame is transformed into the frequency-domain from time-domain by applying fast Fourier transform (FFT).

References

Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5709

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.