現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Help with time variation graphs versus time
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1782405/image.png)
1 件のコメント
採用された回答
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1782460/image.png)
その他の回答 (2 件)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1782445/image.png)
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
![](/matlabcentral/answers/uploaded_files/1782510/IMG_7998.jpeg)
![](/matlabcentral/answers/uploaded_files/1782505/IMG_7997.jpeg)
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.
参考
カテゴリ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)