Sie verfolgen jetzt diese Frage
- Aktualisierungen können Sie in Ihrem Feed verfolgter Inhalte sehen.
- Je nach Ihren Kommunikationseinstellungen können Sie auch E-Mails erhalten.
Help with time variation graphs versus time

1 Kommentar
-
Verknüpfen
Direkter Link zu diesem Kommentar
Akzeptierte Antwort

Weitere Antworten (2)

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.
Siehe auch
Kategorien
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
