@@ -627,3 +627,29 @@ def test_plot_spectrum_array_with_bads():
627627 spectrum .get_data (exclude = ()), spectrum .info , spectrum .freqs
628628 )
629629 spectrum2 .plot (spatial_colors = False )
630+
631+
632+ @pytest .mark .parametrize ("dB" , (False , True ))
633+ @pytest .mark .parametrize ("amplitude" , (False , True ))
634+ def test_plot_spectrum_dB (raw_spectrum , dB , amplitude ):
635+ """Test that we properly handle amplitude/power and dB."""
636+ idx = 7
637+ power = 3
638+ freqs = np .linspace (1 , 100 , 100 )
639+ data = np .full ((1 , freqs .size ), np .finfo (float ).tiny )
640+ data [0 , idx ] = power
641+ info = create_info (ch_names = ["delta" ], sfreq = 1000 , ch_types = "eeg" )
642+ psd = SpectrumArray (data = data , info = info , freqs = freqs )
643+ with pytest .warns (RuntimeWarning , match = "Channel locations not available" ):
644+ fig = psd .plot (dB = dB , amplitude = amplitude )
645+ trace = list (
646+ filter (lambda x : len (x .get_data ()[0 ]) == len (freqs ), fig .axes [0 ].lines )
647+ )[0 ]
648+ got = trace .get_data ()[1 ][idx ]
649+ want = power * 1e12 # scaling for EEG (V → μV), squared
650+ if amplitude :
651+ want = np .sqrt (want )
652+ if dB :
653+ want = (20 if amplitude else 10 ) * np .log10 (want )
654+
655+ assert want == got , f"expected { want } , got { got } "
0 commit comments