You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Help with time variation graphs versus time
Answers (3)
Hi @Jose Martinez ,
You mentioned,”Can you help me on how I can obtain this type of graphs (see example), which represent amplitude variations at a certain frequency with respect to time. Note that the Y axis is in logarithmic.“
Please see my response to your comments below. Please see example code snippet provided below. It generates a spectrogram from your dataset, which includes frequency and amplitude values.
% Sample data formatted as cell array data = { '2010/01/01 00:00', 0.109864, 2.04021; '2010/01/01 00:00', 0.122071, 2.8937; '2010/01/01 00:00', 0.134278, 2.84502; '2010/01/01 00:00', 0.146485, 2.92267; '2010/01/01 00:00', 0.158692, 3.11156; '2010/01/01 00:00', 0.170899, 3.41533; '2010/01/01 00:00', 0.183107, 3.10193; '2010/01/01 00:00', 0.195314, 3.32969; '2010/01/01 00:00', 0.207521, 3.29483; '2010/01/01 00:00', 0.219728, 3.21573 };
% Extracting time and amplitude time = datenum(data(:,1)); % Convert date strings to serial date numbers amplitude = cell2mat(data(:,3)); % Amplitude column as numeric
% Convert amplitude to logarithmic scale (in dB) log_amplitude = 20 * log10(amplitude);
% Define parameters for spectrogram window = hamming(5); % Window length (number of samples) noverlap = 2; % Number of overlapping samples nfft = max(256,2^nextpow2(length(window))); % FFT points
% Create the spectrogram [s,f,t] = spectrogram(log_amplitude, window, noverlap, nfft);
% Plotting figure;
% Spectrogram subplot(2,1,1); % Create subplot for spectrogram imagesc(t, f, abs(s)); % Use abs(s) to get magnitude for visualization axis xy; % Flip y-axis for correct orientation xlabel('Time (Years)'); ylabel('Frequency (Hz)'); title('Spectrogram'); colorbar; % Add color bar to indicate amplitude levels set(gca,'YScale','log'); % Set Y-axis to logarithmic scale
% Update x-axis ticks to show years (example) xticks(datenum({'2009-12-31', '2010-06-30', '2011-12-31'})); % Example ticks xticklabels({'2009', '2010', '2011'});
% Waterfall Plot subplot(2,1,2); % Create subplot for waterfall plot waterplot(s,f,t); % Call the provided function to create the waterfall plot
% Function to create waterfall plot of spectrogram function waterplot(s,f,t) waterfall(f,t,abs(s)'.^2); % Transpose s for correct orientation set(gca,'XDir','reverse','View',[30 50]); % Set view angle xlabel("Frequency (Hz)"); ylabel("Time (s)"); title('Waterfall Plot'); end
Let me break down the code step-by-step to understand how it meets yours requirements and to clarify any potential improvements.
Data Preparation
The first part of the code initializes the sample data as a cell array. This format allows for mixed data types, which is useful for handling date strings alongside numeric values.
data = { '2010/01/01 00:00', 0.109864, 2.04021; '2010/01/01 00:00', 0.122071, 2.8937; ... '2010/01/01 00:00', 0.219728, 3.21573 };
Extracting Time and Amplitude
The code extracts the time and amplitude from the dataset. The datenum function converts date strings into serial date numbers, which MATLAB can process for time-based plotting. The amplitude values are converted from a cell array to a numeric array using cell2mat.
time = datenum(data(:,1)); % Convert date strings to serial date numbers amplitude = cell2mat(data(:,3)); % Amplitude column as numeric
Logarithmic Transformation
To meet the user's requirement of displaying the Y-axis in a logarithmic scale, the amplitude values are transformed into decibels (dB) using the formula 20 * log10(amplitude. This transformation is crucial for visualizing amplitude variations effectively.
log_amplitude = 20 * log10(amplitude);
Spectrogram Parameters
The code defines parameters for the spectrogram, including the window length, overlap, and the number of FFT points. The hamming window is commonly used for spectral analysis due to its favorable properties in reducing spectral leakage.
window = hamming(5); % Window length (number of samples) noverlap = 2; % Number of overlapping samples nfft = max(256,2^nextpow2(length(window))); % FFT points
Generating the Spectrogram
The spectrogram function computes the spectrogram of the logarithmic amplitude data. It returns the complex values of the spectrogram, along with frequency and time vectors.
[s,f,t] = spectrogram(log_amplitude, window, noverlap, nfft);
For more information on this function, please refer to
https://www.mathworks.com/help/signal/ref/spectrogram.html
Plotting the Spectrogram
The code creates a figure with two subplots: one for the spectrogram and another for a waterfall plot. The spectrogram is visualized using imagesc, which displays the magnitude of the spectrogram. The Y-axis is set to a logarithmic scale using set(gca,'YScale','log'), fulfilling the user's requirement.
subplot(2,1,1); % Create subplot for spectrogram imagesc(t, f, abs(s)); % Use abs(s) to get magnitude for visualization axis xy; % Flip y-axis for correct orientation xlabel('Time (Years)'); ylabel('Frequency (Hz)'); title('Spectrogram'); colorbar; % Add color bar to indicate amplitude levels set(gca,'YScale','log'); % Set Y-axis to logarithmic scale
Customizing the X-Axis
The code customizes the x-axis ticks to represent years, enhancing the readability of the time axis.
xticks(datenum({'2009-12-31', '2010-06-30', '2011-12-31'})); % Example ticks xticklabels({'2009', '2010', '2011'});
Waterfall Plot
The second subplot is a waterfall plot, which provides a three-dimensional view of the spectrogram data. The waterplot function is defined to create this visualization.
subplot(2,1,2); % Create subplot for waterfall plot waterplot(s,f,t); % Call the provided function to create the waterfall plot
Please see attached
In nutshell, this code generates a spectrogram that represents amplitude variations at specific frequencies over time, with the Y-axis displayed in a logarithmic scale. The transformation of amplitude to a logarithmic scale, along with the appropriate plotting functions, makes sure that your requirements are met. If you wish to further refine the spectrogram or customize the visual output, you may consider adjusting the window length, overlap, or the frequency range displayed.
Hope this helps resolve your problem. Please let me know if you have any further questions.
See Also
Categories
Products
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.
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
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)